title: select checkboxes dynamically asp net
body: <p>I want to select <code>checkbox</code> dynamically of a <code>datagrid</code>, if <code>datagrid</code> contains the matching <code>value</code> in <code>dataset</code>. I m binding <code>datagrid</code> when  user selects a value of a <code>dropdownlist</code>.</p>
  
  <p>To achieve this I have written following code.</p>
  
  <pre><code>protected void dgAvailableCandidates_ItemDataBound(object sender, DataGridItemEventArgs e)
      {
          DataSet dsSelectedCandidates = (DataSet)Session["SelectedCandidatesDataSet"];
  
          if (dsSelectedCandidates.Tables[1].Rows.Count &gt; 0)
          {
              for (int i = 0; i &lt; dgAvailableCandidates.Items.Count; i++)
              {
                 string indRow = dgAvailableCandidates.DataKeys[i].ToString();
  
                 if (indRow == dsSelectedCandidates.Tables[1].Rows[i]["fk_recId"].ToString())
                 {
                      CheckBox shouldCheck = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
                      shouldCheck.Checked = true;
                 }
  
               }
          }
  
      }
  </code></pre>
  
  <p>I have created a stored procedure that returns two <code>datatables</code>, <code>tables0</code> is to <code>bind</code> <code>datagrid</code> and <code>table1</code> holds <code>recordIds</code> that are already available in <code>database table</code> as <code>selected or bit value (1)</code>.</p>
  
  <p>My issue is here, when <code>datagrid</code> doesn't find a <code>value</code> in <code>table1</code> it throws an <code>exception</code> 'there is no matching row'. How should I fix this <code>exception</code></p>
  
  <p>Thanks!</p>