C# 在Windows窗体应用程序上双击DataGridView时选中列表框特定项

C# 在Windows窗体应用程序上双击DataGridView时选中列表框特定项,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在使用CheckedListBox,以便用户可以为此选择多个项目。我正在从数据库动态填充CheckedListBox。这里是CheckedListBox填充方法 public void FillSubjectsCombo() { DataTable dt = objSubCls.FillSubjects(); chkLstBxClass_FrmSubjecClassRelation.DataSource = dt; chk

我正在使用CheckedListBox,以便用户可以为此选择多个项目。我正在从数据库动态填充CheckedListBox。这里是CheckedListBox填充方法

     public void FillSubjectsCombo()
      {
        DataTable dt = objSubCls.FillSubjects();
        chkLstBxClass_FrmSubjecClassRelation.DataSource = dt;
        chkLstBxClass_FrmSubjecClassRelation.DisplayMember = "Subjects";
        chkLstBxClass_FrmSubjecClassRelation.ValueMember = "SubId";
        chkLstBxClass_FrmSubjecClassRelation.Enabled = true;

          for (int i = 0; i < dt.Rows.Count; i++) 
          {
            //Here i am setting Every item Checked
            chkLstBxClass_FrmSubjecClassRelation.SetItemCheckState(i, CheckState.Checked);
          }
      }
我的问题:双击DataGridView时如何检查特定项

更新:我像这样绑定我的DataGridView

  for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgv_FrmSmstrClsAssign.Rows.Add();
                dgv_FrmSmstrClsAssign.Rows[i].Cells[0].Value = dt.Rows[i].ItemArray[0];//Acadmc Yr
                dgv_FrmSmstrClsAssign.Rows[i].Cells[1].Value = dt.Rows[i].ItemArray[1];// Semester Name
                dgv_FrmSmstrClsAssign.Rows[i].Cells[2].Value = dt.Rows[i].ItemArray[2]; //College 
                dgv_FrmSmstrClsAssign.Rows[i].Cells[3].Value = dt.Rows[i].ItemArray[3];//Class

                dgv_FrmSmstrClsAssign.Rows[i].Cells[4].Value = dt.Rows[i].ItemArray[4]; //Entry Date
                dgv_FrmSmstrClsAssign.Rows[i].Cells[5].Value = dt.Rows[i].ItemArray[5];//IsActive
                dgv_FrmSmstrClsAssign.Rows[i].Cells[6].Value = dt.Rows[i].ItemArray[6];//AcadmicYr Id
                dgv_FrmSmstrClsAssign.Rows[i].Cells[7].Value = dt.Rows[i].ItemArray[7];//Semster Id
                dgv_FrmSmstrClsAssign.Rows[i].Cells[8].Value = dt.Rows[i].ItemArray[8];//Semster Id

            }
for(int i=0;i
我找不到任何允许您轻松映射绑定值的方法,因此您必须使用收集方法获取索引,然后手动选中并取消选中项目

要从
DataGridView
行获取绑定项,可以使用属性:

private void CheckSelectedItem()
{
    // Get bound item object from datagrid
    object item = dgv_FrmSubjectClassRelation.CurrentRow.DataBoundItem;

    // Get corresponding index in listView
    Int32 itemIndexInCheckedListView = chkLstBxClass_FrmSubjecClassRelation.Items.IndexOf(item);

    // Check the item in listView
    chkLstBxClass_FrmSubjecClassRelation.SetItemCheckState(itemIndexInCheckedListView,
          CheckState.Checked);
}

private void dgv_FrmSubjectClassRelation_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    string classId = dgv_FrmSubjectClassRelation.CurrentRow.Cells[3].Value.ToString();
    string className = dgv_FrmSubjectClassRelation.CurrentRow.Cells[4].Value.ToString();

    foreach (int i in chkLstBxClass_FrmSubjecClassRelation.CheckedIndices)
    {
        //Here I am UnChecking Every Checked Item 
        chkLstBxClass_FrmSubjecClassRelation.SetItemCheckState(i, CheckState.Unchecked);
    }

    // --------------Check the selected item----------------
    this.CheckSelectedItem();
}
编辑:

您所做的并不完全是绑定(好吧,它是绑定的,只是不一样),所以前面的解决方案不适合您。如果DataTable和DataGridView都包含主键或其他唯一标识符,则可以将CurrentRow映射到DataTable中的项:

private void CheckSelectedItem()
{
    // Get bound item object from datagrid
    object uniqueKey = dgv_FrmSubjectClassRelation.
        CurrentRow.
        Cells["SOME UNIQUE VALUE COLUMN"].
        Value;

    // Adapting http://stackoverflow.com/a/9300237/3745022 - there are more simple LINQless 
    // solutions for this situation, but it is not important for the concept.
    Int32 itemIndexInCheckedListView = chkLstBxClass_FrmSubjecClassRelation.
        Items.
        Select((value, index) => new { value, index }).
        Where(pair => pair.value.UniqueValue == uniqueKey ).
        Select(pair => pair.index + 1).
        FirstOrDefault() - 1;

    // Check the item in listView
    chkLstBxClass_FrmSubjecClassRelation.SetItemCheckState(itemIndexInCheckedListView,
          CheckState.Checked);
}
如果您没有这样的唯一列,您可能需要添加它(只需将其隐藏)


更好-使用全面的数据绑定

DataGridViewCellEventArgs
具有属性
RowIndex
。您是否尝试使用此索引获取复选框id?查看或查找某些列表框中的项目。当然,这取决于您是否知道这两个控件中的值之间的关系。数据表是:
classId
是否与
SubId
相同。。。。我对DataGridView没有任何问题我想将CheckedListBox的特定项设置为选中状态……DataTable是否有一些主键(或另一个唯一值)可以方便地识别行?是的,我有主键对象项=dgv_FrmSubjectClassRelation.CurrentRow.DataBoundItem;它给了我零value@KhurramAli嗯,是将数据绑定到datagridview还是手动添加?或者可能是CurrentRow为空(这几乎不可能,但无论如何)?检查我更新的问题我接受你的答案,因为我已经解决了我的问题,类似于你的asnswer…..Thmkxxx
private void CheckSelectedItem()
{
    // Get bound item object from datagrid
    object uniqueKey = dgv_FrmSubjectClassRelation.
        CurrentRow.
        Cells["SOME UNIQUE VALUE COLUMN"].
        Value;

    // Adapting http://stackoverflow.com/a/9300237/3745022 - there are more simple LINQless 
    // solutions for this situation, but it is not important for the concept.
    Int32 itemIndexInCheckedListView = chkLstBxClass_FrmSubjecClassRelation.
        Items.
        Select((value, index) => new { value, index }).
        Where(pair => pair.value.UniqueValue == uniqueKey ).
        Select(pair => pair.index + 1).
        FirstOrDefault() - 1;

    // Check the item in listView
    chkLstBxClass_FrmSubjecClassRelation.SetItemCheckState(itemIndexInCheckedListView,
          CheckState.Checked);
}