数据显示操作:
dgBill.Columns[0].DataPropertyName = "key1";
dgBill.Columns[1].DataPropertyName = "key2";
dgBill.Columns[2].DataPropertyName = "key3";
DataTable dt = new DataTable();
dt.Columns.Add("key1", Type.GetType("System.String"));
dt.Columns.Add("key2", Type.GetType("System.String"));
dt.Columns.Add("key3", Type.GetType("System.String"));
DataRow dr = null;
dr = dt.NewRow();
dr["key1"] = "key1";
dr["key1"] = "key2";
dr["key1"] = "key3";
dt.Rows.Add(dr);
this.dgBill.DataSource = dt;
选择一行数据:
//右键菜单事项
private void dg1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex >= 0)
{
//若行已是选中状态就不再进行设置
dg1.ClearSelection();
dg1.Rows[e.RowIndex].Selected = true;
//设置当前行
dg1.CurrentCell = dg1.Rows[e.RowIndex].Cells[0];
//弹出操作菜单
contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
}
}
}
//选择菜单的时候,进行获取右键一行的数据
DataGridViewRow rowData = dgBill.CurrentRow;
if (rowData != null)
{
if (rowData.Cells["ID"].Value != null)
{
}
else
{
MessageBox.Show("数据转换异常", "提示", MessageBoxButtons.OK);
}
}