Set SelectedIndex for DataGridViewComboBoxCell

Đoạn code sau hướng dẫn cách thiết lập DataGridViewComboBox Column trong windows Forms

DataTable dt = null;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(“Category”);
DataRow dr = dt.NewRow();
dr[“Category”] = “Select All”;
dt.Rows.Add(dr);
DataRow row = dt.NewRow();
row[0] = “Cat 1”;
dt.Rows.Add(row);
DataRow row2 = dt.NewRow();
row2[0] = “Cat 2”;
dt.Rows.Add(row2);
DataGridViewComboBoxColumn newColumn = new DataGridViewComboBoxColumn();
newColumn.Name = “Category”;
newColumn.DataSource = dt;
newColumn.DisplayMember = “Category”;
newColumn.ValueMember = “Category”;
dataGridView1.Columns.Add(newColumn);
}

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = dt.Rows[0][0].ToString();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s