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

本文关键字:列表 应用 窗体 Windows 应用程序 程序上 DataGridView 双击 | 更新日期: 2023-09-27 18:18:26

我正在使用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);
          }
      }

在相同的Windows窗体上,我有DataGridView我想当我双击数据网格的任何一行,然后从选定的行获取值,并从该值使受尊重的项目在CheckedListBox和其他项目选中

这是DataGridView事件

 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);
        }

我的问题:如何检查特定项目时双击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
            }

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

我无法找到任何允许您轻松映射绑定值的方法,因此您必须使用Items集合的IndexOf方法来获取索引,然后手动勾选取消勾选项。

DataGridView行获取绑定项可以使用DataGridViewRow。DataBoundItem属性:

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();
}
编辑:

你所做的并不是真正的绑定(好吧,它是绑定,只是不像Windows窗体定义的那样),所以前面的解决方案对你不起作用。如果DataTable和DataGridView都包含主键或其他唯一标识符,那么可以将CurrentRow映射到DataTable中的Item:

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);
}

如果您没有这样的唯一列,您可能需要添加它(只需将其隐藏)

更好-使用完整的数据绑定- http://msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.90).aspx;