是否可以仅在datagridview中的某些行添加CheckBoxcolumn [英] Is it possible to add CheckBoxcolumn only at certain rows in a datagridview

查看:85
本文介绍了是否可以仅在datagridview中的某些行添加CheckBoxcolumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将具有以下数据网格视图

I will have my data grid view as follows

RecordTypeCode   Content
   FileHeader      1111111111111
   BatchHeader     5666666666666
   EntryDetail     656546545644545
   BatchControl    8654654564564
   FileControl     945645


我想只在BatchHeader和EntryDetail上有复选框,这是可能的.这是我将数据绑定到数据网格视图的方式


I would like to have check boxes only at BatchHeader and EntryDetail is it possible. This is the way i am binding data to data grid view

if (line.StartsWith("1"))
{
  dcID = new DataColumn("RecordTypeCode");
  dt.Columns.Add(dcID);
  DataColumn dcSomeText = new DataColumn("Content");
  dt.Columns.Add(dcSomeText);
  dr = dt.NewRow();
  dr["RecordTypeCode"] = filecontrolvariables.rectype[line.Substring(0, 1)].ToString();
  dr["Content"] = line;
  dt.Rows.Add(dr);
}

if (line.StartsWith("5"))
{
   dr = dt.NewRow();
   dr = dt.NewRow();
  dr["RecordTypeCode"] = filecontrolvariables.rectype[line.Substring(0, 1)].ToString();
  dr["Content"] = line;
 dt.Rows.Add(dr);
}



我想根据需要添加复选框,有没有人可以帮助我以其他方式尝试过此操作



I would like to add check boxes as per needed can any one help me I tried this in different ways

if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
  if (dataGridView2.Rows[e.RowIndex].Cells["RecordTypeCode"].Value.ToString() == "BatchHeader")
 {
    e.PaintBackground(e.ClipBounds, true);
     e.Handled = true;
  }
 }

 int columnIndex = -1;
 int rowIndex = -1;
 if (dataGridView2.CurrentCell != null)
 {
     columnIndex = dataGridView2.CurrentCell.ColumnIndex;
     rowIndex = dataGridView2.CurrentCell.RowIndex;
  }
   if (columnIndex == 0)
   {
     if (e.RowIndex >= 0)
    {
    if (dataGridView2.Rows[e.RowIndex].Cells["RecordTypeCode"].Value.ToString() == "Batch Header")
    {
      e.PaintBackground(e.ClipBounds, true);
      e.Handled = true;
     }
     }
   }


但是当我仅对BatchHeader进行检查时,我的对应行显示为空


But my corresponding row is showing empty when i check it only for BatchHeader

推荐答案

这是解决方案

Here is the solution

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                //if (e.ColumnIndex == <index of="" your="" checkbox="" column=""> && (e.RowIndex == 1 || e.RowIndex == 2))
                if (dataGridView2.Rows[e.RowIndex].Cells["RecordTypeCode"].Value.ToString() == "Batch Header" && e.ColumnIndex == <index of="" your="" checkbox="" column="" should="" be="" 0="" sicne="" its="" the="" first="">)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
            }
        }</index></index>


这篇关于是否可以仅在datagridview中的某些行添加CheckBoxcolumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆