數據顯示操作:
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);
}
}