DataGridView 绑定数据源(DataSource)后新增列的索引为0

问题1:

DataGridView(以下简称dgv)在绑定DataSource后新增一列,运行显示在最后一列,但输出索引却显示0。

解决办法:

dgvInfo.AutoGenerateColumns = false;

dgvInfo.Columns.Add(new DataGridViewTextBoxColumn()
{
    HeaderText = "Status",
    Name = "Status",
    Visible = false
});

Status列被隐藏,索引为2,Status2没有被隐藏,索引为3

本人也不清楚为什么,去问了大模型,这是大模型的回答:

这是因为在设置dgvInfo.AutoGenerateColumns = false;后,DataGridView不再自动生成列。这意味着你需要手动添加所有需要的列。在这种情况下,当你添加一个新的TextBoxColumn时,它会被添加到现有的列之后,因此索引为2。

而当你设置dgvInfo.AutoGenerateColumns = true;时,DataGridView会自动生成列。这意味着当你添加一个新的TextBoxColumn时,它会被添加到自动生成的列之前,因此索引为0。

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以通过以下步骤实现在DataGridView中添加按钮并绑定事件: 1. 在DataGridView中添加一个DataGridViewButtonColumn,用来放置按钮。 ```csharp DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); buttonColumn.HeaderText = "操作"; buttonColumn.Text = "删除"; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Add(buttonColumn); ``` 2. 在DataGridView的CellClick事件中添加按钮点击事件处理程序。 ```csharp private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == dataGridView1.Columns["操作"].Index) { //处理按钮点击事件 MessageBox.Show("您点击了第 " + e.RowIndex + " 行的删除按钮"); } } ``` 3. 将数据源绑定DataGridView。 ```csharp DataTable dt = new DataTable(); dt.Columns.Add("编号"); dt.Columns.Add("姓名"); dt.Rows.Add("001", "张三"); dt.Rows.Add("002", "李四"); dataGridView1.DataSource = dt; ``` 完整的代码示例: ```csharp public partial class Form1 : Form { public Form1() { InitializeComponent(); InitDataGridView(); } private void InitDataGridView() { //添加按钮 DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); buttonColumn.HeaderText = "操作"; buttonColumn.Text = "删除"; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Add(buttonColumn); //绑定数据源 DataTable dt = new DataTable(); dt.Columns.Add("编号"); dt.Columns.Add("姓名"); dt.Rows.Add("001", "张三"); dt.Rows.Add("002", "李四"); dataGridView1.DataSource = dt; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == dataGridView1.Columns["操作"].Index) { //处理按钮点击事件 MessageBox.Show("您点击了第 " + e.RowIndex + " 行的删除按钮"); } } } ``` ### 回答2: 在DataGridView中为每行添加按钮,可以通过DataGridView的CellFormatting事件来实现。具体步骤如下: 1. 首先,确保已经将数据源绑定DataGridView上,可以通过设置DataGridViewDataSource属性来实现。 2. 在DataGridView的CellFormatting事件中添加代码,该事件会在每个单元格绘制时触发。 3. 在CellFormatting事件中,首先判断当前单元格所在是否为按钮,可以通过索引或者名称进行判断。例如,若按钮是第一,可以使用以下代码判断: ``` if (e.ColumnIndex == 0) { // 添加按钮代码 } ``` 4. 在判断当前单元格所在为按钮后,判断当前单元格所在行是否是数据行,而非标题行或者其他特殊行。可以通过判断单元格所在行的索引进行判断。 ``` if (e.RowIndex >= 0) { // 添加按钮代码 } ``` 5. 在判断为数据行后,创建一个按钮控件,并将其添加到当前单元格的控件集合中。 ``` if (e.RowIndex >= 0) { DataGridViewButtonCell buttonCell = new DataGridViewButtonCell(); dataGridView.Rows[e.RowIndex].Cells[0] = buttonCell; } ``` 6. 此时,按钮已经添加到了DataGridView的每一行上。 完整代码示例如下所示: ``` private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex >= 0) { DataGridViewButtonCell buttonCell = new DataGridViewButtonCell(); dataGridView.Rows[e.RowIndex].Cells[0] = buttonCell; } } ``` 注意:该示例假设按钮为第一,若按钮索引不是0时,需要根据实际情况进行修改。另外,可以根据需求对按钮样式、事件等进行定制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值