2
Answers

Error: datagridviewcomboboxcell value is valid

mahmoud omar

mahmoud omar

8y
265
1
<hr /><div id="pastingspan1" style="display: inline-block;"><div id="pastingspan1"><pre lang="C#" id="pre287282" shrunk="true" processed="true" class="notranslate" style="padding: 6px; font-stretch: normal; font-size: 9pt; font-family: Consolas, &quot;Courier New&quot;, Courier, mono; white-space: pre-wrap; border: 1px solid #fbedbb; tab-size: 4; overflow: auto; word-wrap: break-word; word-break: break-word; margin-top: 0px; height: 400px; background-color: #fbedbb;"><span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">void</span> FillComboboxCarPlace() { <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">try</span> { dSet = <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">new</span> DataSet(); <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">if</span> (con.State == ConnectionState.Closed) { con.Open(); } s = <span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">"</span><span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">select Move_id , Move_Place from Car_Move_Place"</span>; sCommand = <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">new</span> SqlCommand(s, con); sdAdapter = <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">new</span> SqlDataAdapter(); sdAdapter.SelectCommand = sCommand; sdAdapter.Fill(dSet); DataRow dr = dSet.Tables[<span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">0</span>].NewRow(); dr.ItemArray = <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">new</span> <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">object</span>[<span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">2</span>] { <span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">0</span>, <span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">"</span><span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;"> ---Select--- "</span> }; dSet.Tables[<span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">0</span>].Rows.InsertAt(dr, <span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">0</span>); <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">foreach</span> (DataGridViewRow row <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">in</span> dataGridView1.Rows) { <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">int</span> index = <span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">0</span>; <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">var</span> cbxMove = row.Cells[index] <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">as</span> DataGridViewComboBoxCell; <span class="code-comment" style="margin: 0px; padding: 0px; border: 0px; color: #008000; font-style: italic;">//</span><span class="code-comment" style="margin: 0px; padding: 0px; border: 0px; color: #008000; font-style: italic;">DataGridViewComboBoxCell ContactCombo = (DataGridViewComboBoxCell)(row.Cells["Car_Move_Place"]); </span> ComboMovePlace.ValueMember = <span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">"</span><span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">Move_id"</span>; ComboMovePlace.DisplayMember = <span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">"</span><span class="code-string" style="margin: 0px; padding: 0px; border: 0px; color: #800080;">Move_Place"</span>; ComboMovePlace.DataSource = dSet.Tables[<span class="code-digit" style="margin: 0px; padding: 0px; border: 0px; color: #000080;">0</span>]; } <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">if</span> (con.State == ConnectionState.Open) { con.Close(); } &nbsp; } <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">catch</span> { <span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px; color: #0000ff;">return</span>; } }</pre><pre lang="C#" id="pre287282" shrunk="true" processed="true" class="notranslate" style="padding: 6px; font-stretch: normal; font-size: 9pt; font-family: Consolas, &quot;Courier New&quot;, Courier, mono; white-space: pre-wrap; border: 1px solid #fbedbb; tab-size: 4; overflow: auto; word-wrap: break-word; word-break: break-word; margin-top: 0px; height: 400px; background-color: #fbedbb;"> private void btn_Search_Click(object sender, EventArgs e) { try { dataGridView1.Rows.Clear(); dataGridView1.Refresh(); string s1 = @"SELECT COUNT(*) FROM Move_Transfer WHERE fromDate = @fromDate"; if (con.State == ConnectionState.Closed) { con.Open(); } sCommand = new SqlCommand(s1, con); sCommand.Parameters.AddWithValue("@fromDate", dateTimePicker_form.Text); int records = (int)sCommand.ExecuteScalar(); if (records == 0) { s = " SELECT Place_desc ,Car_id "; s = s + " FROM Cars "; s = s + " INNER JOIN Place ON Cars.Place_id = Place.Place_id "; sCommand = new SqlCommand(s, con); sdAdapter = new SqlDataAdapter(); sdAdapter.SelectCommand = sCommand; dt = new DataTable(); sdAdapter.Fill(dt); foreach (DataRow item in dt.Rows) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item["Place_desc"].ToString(); dataGridView1.Rows[n].Cells[1].Value = item["Car_id"].ToString(); FillComboboxCarPlace(); } } else { s = " SELECT Place.Place_desc, Move_Transfer.Car_id, Move_Transfer.busy, Move_Transfer.damage, Move_Transfer.withoutDriver, Car_Move_Place.Move_Place "; s = s + " FROM Move_Transfer "; s = s + " INNER JOIN Cars "; s = s + " ON Move_Transfer.Car_id = Cars.Car_id INNER JOIN "; s = s + " Place ON Cars.Place_id = Place.Place_id INNER JOIN "; s = s + " Car_Move_Place ON Move_Transfer.Move_id = Car_Move_Place.Move_id "; s = s + " where fromDate = '" + dateTimePicker_form.Text + "' "; sCommand = new SqlCommand(s, con); sdAdapter = new SqlDataAdapter(); sdAdapter.SelectCommand = sCommand; dt = new DataTable(); sdAdapter.Fill(dt); foreach (DataRow item in dt.Rows) { int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item["Place_desc"].ToString(); dataGridView1.Rows[n].Cells[1].Value = item["Car_id"].ToString(); dataGridView1.Rows[n].Cells[2].Value = item["busy"].ToString(); dataGridView1.Rows[n].Cells[3].Value = item["damage"].ToString(); dataGridView1.Rows[n].Cells[4].Value = item["withoutDriver"].ToString(); dataGridView1.Rows[n].Cells[5].Value = item["Move_Place"].ToString(); } FillComboboxCarPlace(); btn_Search.Enabled = false; } if (con.State == ConnectionState.Open) { con.Close(); } } catch { return; } }<br /></pre></div></div>

Answers (2)