自己只用其中最简单的部分,不涉及数据库,代码如下
1、写一行数据
/// <summary>
/// 设置行数据
/// </summary>
void SetRowVal()
{
int rowIndx = paraDgv.Rows.Add();
DataGridViewRow tempRow = paraDgv.Rows[rowIndx];
tempRow.Cells[0].Value = "haha0";
//---------checkbox
DataGridViewCheckBoxCell tempRowCb = (DataGridViewCheckBoxCell)tempRow.Cells[1];
tempRowCb.Value = false;
//---------combox
DataGridViewComboBoxCell tempRowCmb = (DataGridViewComboBoxCell)tempRow.Cells[2];
tempRowCmb.Items.Clear();
tempRowCmb.Items.Add("a1");
tempRowCmb.Items.Add("a2");
tempRowCmb.Items.Add("a3");
tempRowCmb.Items.Add("a4");
tempRowCmb.Value = "a3"; //设置的值必须在items中,否则报错
//tempRow.Cells[1].Value = "haha1";
//tempRow.Cells[2].Value = "haha2";
//---------img
DataGridViewImageCell tempImgCell = (DataGridViewImageCell)tempRow.Cells[3];
tempImgCell.Value = glRedBmp;
}
2、读取一行数据
/// <summary>
/// 读取行数据
/// </summary>
/// <param name="theRowIndex"></param>
void GetRowVal(int theRowIndex)
{
DataGridViewRow tempRow = paraDgv.Rows[theRowIndex];
//---------textbox.通过两种方法获取对应的值,一个通过列表的名,一个是通过固定索引
var aa = tempRow.Cells[textCol.Name].Value;
string str1 = tempRow.Cells[0].Value.ToString();
//---------checkbox
DataGridViewCheckBoxCell tempRowCb = (DataGridViewCheckBoxCell)tempRow.Cells[1];
bool theB = (bool)tempRowCb.Value;
//---------combox
DataGridViewComboBoxCell tempRowCmb = (DataGridViewComboBoxCell)tempRow.Cells[2];
string str3 = tempRowCmb.Value.ToString();
}
3、被选中的行
void GetSelectedRows()
{
//当前被选中的行
if (paraDgv.SelectedRows.Count != 1) {
MessageBox.Show("只允许选择一行数据");
return;
}
int indx = paraDgv.CurrentRow.Index;
}