Datagridview - 在第一次运行后添加新列 - 索引超出范围 [英] Datagridview - Add new Column - Index out of Range after the first run

查看:165
本文介绍了Datagridview - 在第一次运行后添加新列 - 索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,我正在构建一个SQL数据库的数据网格。


由于我需要4列来显示一个组合框而不是普通的文本单元,我创建所说的comboboxcells并为gridview创建了新列。


当我第一次运行程序时,一切都完全正常,所有数据和组合框都没问题。

如果我然后推动"刷新"按钮,它调用在程序启动时运行的相同功能,当我想用​​组合框添加新列时,我得到索引超出范围错误。


我搜索了现在几个小时,只是找不到原因,所有变量似乎在启动和刷新时都具有相同的值。


插入时第二行的代码在这一点断开:
$




 DataGridViewColumn seccolumn = new DataGridViewColumn(secbox); 
dataGridView1.Columns.Insert(18,seccolumn);
dataGridView1.Columns [18] .HeaderText = dataGridView1.Columns [9] .HeaderText;
dataGridView1.Columns [9] .Visible = false;

在尝试插入指定索引之前,我有这样的代码:

$

 DataGridViewColumn seccolumn = new DataGridViewColumn(secbox); 
dataGridView1.Columns.Add(seccolumn);
dataGridView1.Columns [18] .HeaderText = dataGridView1.Columns [9] .HeaderText;
dataGridView1.Columns [9] .Visible = false;

使用的变量"secbox"是填充的combocellbox,在两种情况下它都保存相同的数据(在第一个rund和刷新时)


该函数也会在构建它之前清除gridview,如下所示:



 dataGridView1.DataSource = null; 
dataGridView1.ClearSelection();
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
dataGridView1.DataBindings.Clear();
dataGridView1.Refresh();


任何提示都会有所帮助!

提前致谢!

解决方案

你好,


使用序数访问列的索引是不正确的,而是给每个DataGridViewColumn(无论类型DataGridViewTextBoxColumn,DataGridViewComboBoxColumn等)一个名称并以这种方式访问​​。现在做完这个调整后没有改变任何东西
else并运行项目我的猜测是你会得到一个类似"列名不存在"的错误,这更容易删除。


如果以上内容没有帮助,并且您对使用DataGridView组合框列的更好方法持开放态度,请参阅使用SQL-Server填充数据的MSDN代码示例。




Hey all, i'm currently building a datagridview out of a SQL database.

Since i need 4 columns to display a combocellbox instead of a normal textcell, i create said comboboxcells and genereate new columns for the gridview.

When i run the program the first time, everything is completly fine, all data and comboboxes are fine.
If i then push the "refresh" button, which calls the same function which is run at startup of the program, i get a index out of range error when i want to add the new columns with the combobox.

I searched for hours now and just can't find the cause of this, all variables seem to have the same value on startup and on refresh.

The code breaks at this point at the second line when inserting:


DataGridViewColumn seccolumn = new DataGridViewColumn(secbox);
                dataGridView1.Columns.Insert(18,seccolumn);
                dataGridView1.Columns[18].HeaderText = dataGridView1.Columns[9].HeaderText;
                dataGridView1.Columns[9].Visible = false;

Before trying to insert with index specified, i had the code this way:

DataGridViewColumn seccolumn = new DataGridViewColumn(secbox);
                dataGridView1.Columns.Add(seccolumn);
                dataGridView1.Columns[18].HeaderText = dataGridView1.Columns[9].HeaderText;
                dataGridView1.Columns[9].Visible = false;

The used variable "secbox" is the filled combocellbox, in both cases it holds the same data (at first rund and when refreshing)

the function also clears the gridview as followed before building it again:

dataGridView1.DataSource = null;
dataGridView1.ClearSelection();
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
dataGridView1.DataBindings.Clear();
dataGridView1.Refresh();

Any tips would be helpfull !
Thanks in advance !

解决方案

Hello,

Using ordinal index to access columns is incorrect, instead give each DataGridViewColumn (no matter the type DataGridViewTextBoxColumn, DataGridViewComboBoxColumn etc) a name and access this way. Now after making this adjustment without changing anything else and run the project my guess is you will get a error something like "column name does not exists" which is easier to take down.

If the above does not assist and you are open to a better way to work with a DataGridView combobox column see my MSDN code sample which uses SQL-Server to populate data.

https://code.msdn.microsoft.com/DataGridViewComboBox-60d3f007?redir=0


这篇关于Datagridview - 在第一次运行后添加新列 - 索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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