hi i was retrieved the data from database and ataching to the gridview but it shows DataGridViewCombobox value is not valid. how to handle this....
This is my code.....
private void OperationsonGridview_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "Select";
chk.Name = "chk";
chk.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
chk.FlatStyle = FlatStyle.Standard;
chk.ThreeState = false;
//Creaeting ComboBox Column for DataGridView:
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "gender";
combo.FlatStyle = FlatStyle.Standard;
//Adding Item to ComBoxColumn:
combo.Items.Add("male");
combo.Items.Add("female");
//Adding button Column for DataGridView:
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
button.HeaderText = "Delete";
button.FlatStyle = FlatStyle.Popup;
button.DefaultCellStyle.BackColor = Color.Red;
button.DefaultCellStyle.ForeColor = Color.White;
//Adding All Column to DataGridView:
dataGridView1.Columns.Insert(0, chk);
dataGridView1.Columns.Add("", "Name");
dataGridView1.Columns.Add("", "Qualification");
dataGridView1.Columns.Insert(3, combo);
dataGridView1.Columns.Insert(4, button);
dataGridView1.Columns.Add("", "ID");
//Hide ID Column:
dataGridView1.Columns[5].Visible = false;
//Assign Width For Columns:
dataGridView1.Columns[0].Width = 70;
dataGridView1.Columns[1].Width = 205;
dataGridView1.Columns[2].Width = 120;
dataGridView1.Columns[3].Width = 190;
dataGridView1.Columns[4].Width = 90;
//Color and Font Design For DataGridView:
dataGridView1.DefaultCellStyle.Font = new Font("Calibri", 10.25f, FontStyle.Regular);
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Calibri", 11, FontStyle.Regular);
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.BurlyWood;
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.RowHeadersVisible = false;
dataGridView1.BackgroundColor = Color.White;
cn.Open();
string sql = "select * from REG_USER";
SqlCommand cmd = new SqlCommand(sql, cn);
SqlDataReader dr = cmd.ExecuteReader();
dataGridView1.Columns[5].Visible = false;
while (dr.Read())
{
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[1].Value = dr["name"].ToString();
row.Cells[2].Value = dr["qualification"].ToString();
//here showing the error because row.Cells[3] have a combobox
row.Cells[3].Value = dr["gender"].ToString();
row.Cells[4].Value = "delete";
row.Cells[5].Value = dr["id"].ToString();
dataGridView1.Columns[5].Visible = false;
dataGridView1.Rows.Add(row);
}
cn.Close();
}
thank you...
Loading

VulpesPosted May 29, 2013, 6:43 AM
So what's probably happening is that the database is coming up with something such as "M", "Male" or null which doesn't match the combobox value "male".
If the problem is just capitalization, you could change it to:
row.Cells[3].Value = dr["gender"].ToString().ToLower();