C#中DataGridView經常使用操作實例小結。本站提示廣大學習愛好者:(C#中DataGridView經常使用操作實例小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中DataGridView經常使用操作實例小結正文
本文實例講述了C#中DataGridView經常使用操作。分享給年夜家供年夜家參考。詳細以下:
public void Binder1()
{
DataTable tableType = DataBase.SQLDBHelper.GetDataTable("select top 200 unit_code,unit_name from unit ");
DataTable table = DataBase.SQLDBHelper.GetDataTable("select top 2 * from TempProduct");
DataGridViewRow dgvr;
foreach (DataRow row in table.Rows)
{
dgvr = new DataGridViewRow();
dgvr.CreateCells(dataGridView);
dgvr.Cells[0].Value = row["Id"].ToString();
dgvr.Cells[1].Value = row["Name"].ToString();
dgvr.Cells[2].Value = row["Age"].ToString();
dgvr.Cells[3].Value = row["Address"].ToString();
//綁定下拉列表
DataGridViewComboBoxColumn dgvcbc = dataGridView.Columns[4] as DataGridViewComboBoxColumn;
if (dgvcbc != null)
{
//綁定上去列表
dgvcbc.DataSource = tableType;
dgvcbc.DisplayMember = "unit_name";
dgvcbc.ValueMember = "unit_code";
}
//為下拉列表設置默許值
dgvr.Cells[4].Value = row["EntryId"].ToString();
//設置復選框能否選中
dgvr.Cells[5].Value = row["flag"].ToString() == "0" ? true : false;
//在列表中找到DataGridViewLinkColumn
DataGridViewLinkColumn links = dataGridView.Columns[6] as DataGridViewLinkColumn;
if (links != null)
{
//須要設置DataGridViewLinkColumn的UseColumnTextForLinkValue屬性為true才會有感化
links.Text = "點擊檢查";
}
//在列表中找到DataGridViewButtonColumn
DataGridViewButtonColumn button = dataGridView.Columns[7] as DataGridViewButtonColumn;
if (button != null)
{
//須要設置DataGridViewButtonColumn的UseColumnTextForLinkValue屬性為true才會有感化
button.Text = "點擊檢查";
}
dataGridView.Rows.Add(dgvr);
}
}
願望本文所述對年夜家的C#法式設計有所贊助。