是否可以仅在 datagridview 中的某些行添加 CheckBoxcolumn
我的数据网格视图如下所示,
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 时,我的相应行显示为空
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 cellpainting 事件来根本不显示复选框。我不确定这在数据绑定期间是否会产生其他后果,但它没有显示我想要的行的复选框。
我已经更新了您需要的方法。
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.
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.