把datagridview 数据源转换为绑定的数据源
public class PlanInfo
{
/// <summary>
/// 计划ID
/// </summary>
public int a { get; set; }
/// <summary>
/// 序号
/// </summary>
public int b { get; set; }
/// <summary>
/// 发车
/// </summary>
public string c { get; set; }
}
List<PlanInfo> planInfos1 = new List<PlanInfo>();
dataGridview 绑定planInfos1,显示列为计划ID,序号。现访问其他列的数据
List<PlanInfo> ps = dataGridview.DataSource as List<PlanInfo>;
PlanInfo p = ps[e.RowIndex];
this.c = p.a;
this.c = p.c;
private void dataGridview_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
{
foreach (DataGridViewRow dr in this.dgvOnVehicle.Rows)
{
dr.Selected = false;
}
this.dataGridview.Rows[e.RowIndex].Selected = true;
DataGridView dgv = (DataGridView)sender;
List<PlanInfo> ps = dgv.DataSource as List<PlanInfo>;
if (ps != null)
{
PlanInfo p = ps[e.RowIndex];
if (p == null) return;
planInfoMenu = p;
this.a = p.a;
this.b = p.b;
this.c = p.c;
}
catch (Exception ex)
{
}
}
}
![](https://img2024.cnblogs.com/blog/35695/202407/35695-20240713070336838-1837943664.jpg)