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

时间:2014-08-17 14:44:49

标签: c# winforms datagridview

我正在使用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我希望当我双击任何一行datagrid然后从选定的行获取值并从该值中选中CheckedListBox和其他项目中的受尊重项目UnChecked

这是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时如何检查特定项目

更新:我正在绑定My DataGridView Like This

  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

            }

1 个答案:

答案 0 :(得分:1)

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

要从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 Forms defines it),所以以前的解决方案对你不起作用。如果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);
}

如果您没有此类唯一列,则可能需要添加它(只是将其隐藏)

OR 甚至更好 - 使用成熟的DataBinding - http://msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.90).aspx;