是否可以仅在 datagridview 中的某些行添加 CheckBoxcolumn

发布于 2024-12-12 02:09:12 字数 1809 浏览 0 评论 0 原文

我的数据网格视图如下所示,

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

我希望仅在 BatchHeader 和 EntryDetail 处有复选框,是否可以。这是我将数据绑定到数据网格视图的方式

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

我想根据需要添加复选框任何人都可以帮助我

时我尝试了这个

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 时,我的相应行显示为空

I will have my data grid view as follows

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

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

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

Sample image when my data binded

enter image description here

I tried this

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-19 02:09:12

您需要使用 cellpainting 事件来根本不显示复选框。我不确定这在数据绑定期间是否会产生其他后果,但它没有显示我想要的行的复选框。

我已经更新了您需要的方法。

 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 column>)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
            }
        }

e.RowIndex == 1 || 的条件e.RowIndex == 2 基于这样的事实:batchheader 和entrydetail 不会改变它们的位置。但如果这是动态的,那么您只需检查记录类型列中的文本以匹配 BatchHeader 和 EntryDetail。

if 语句是最重要的,它检查哪个特定单元格(行、列)是您不希望在其中包含任何复选框的单元格。它们必须与 AND (&&) 一起使用,因为您想要一个特定单元格。如果您所解释的正是您所需要的,您可以按原样使用我编写的代码。您可能需要在 if 语句中为条目详细信息添加一项检查,这需要使用 OR (||) 和批处理标头检查来完成。正是我在代码的注释部分中所做的,特别注意括号。

我在 此链接中看到了解决方案 当我尝试它时,它起作用了。还有其他提到的内容,以防这不适合您想要的。

You need to use the cellpainting event to not display the checkbox at all. I'm not sure if this would have other consquences during data binding but it did not display the checkbox for the rows that I wanted.

I have updated the method for what you would need.

 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 column>)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
            }
        }

The condition for e.RowIndex == 1 || e.RowIndex == 2 is based on the fact that batchheader and entrydetail wont change their position. But if that is dynamic, then you can just check the text in the recordtype column to match BatchHeader and EntryDetail.

The if statement is the most important, it checks which particular cell (row, column) is the one you dont want any checkbox in. They must be used with an AND (&&) as you want one particular cell. You can use the code I have written as is if what you have explained is what you need. You will probably need to add one more check in the if statement for entry detail which needs to be done with an OR (||) with the batch header check. Exactly how I have in the commented part of the code, taking notice to the brackets especially.

I saw the solution at this link which worked when I tried it. There are others mentioned incase this isn't suitable for what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文