本文介绍了dbgrid列索引丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在这段代码中有很多错误,但最突出的是我无法访问.columns [index]方法,除非我手动添加了列。请解释和帮助。请原谅所有注释掉的东西。I have many bugs in this code but most prominently i cant access the .columns[index] method unless i added the column manually. please explain and help. pardon all the commented out stuff. public frmSettings() { { InitializeComponent(); InitializeComponent(); 面板 automation = new 面板 ();Panelautomation =newPanel(); automation.AutoScroll = automation.AutoScroll = true ;true;    / * * Dim aRow As DataRow = ds.Tables(0).NewRow * Dim aRow As DataRow = ds.Tables(0).NewRow aRow(" EmpID")= txtEmpID.TextaRow("EmpID") = txtEmpID.Text aRow(" ; EmpName")= txtEmpName.TextaRow("EmpName") = txtEmpName.Text aRow(" EmpDOB")= txtDOB.TextaRow("EmpDOB") = txtDOB.Text aRow(" EmpDepartment")= txtDept .TextaRow("EmpDepartment") = txtDept.Text ds.Tables(0).Rows.Add(aRow)ds.Tables(0).Rows.Add(aRow) ds.Tables(0).AcceptChanges()ds.Tables(0).AcceptChanges() * / 按钮 update = new 按钮 ();Buttonupdate =newButton(); automation.Controls.Add(更新); automation.Controls.Add(update); update.Text = update.Text = "更新" ; ;"Update";   ;    update.Location = update.Location = new Point (0, 215);newPoint(0, 215);   ;     update.Click + = update.Click += new EventHandler (updateAuto);newEventHandler(updateAuto);    按钮 next = new 按钮 ();Buttonnext =newButton(); automation.Controls.Add(next); automation.Controls.Add(next); next.Text = next.Text = "下一步" ; ;"Next";   ;    next.Location = next.Location = new Point (80, 215);newPoint(80, 215);   ;     next.Click + = next.Click += new EventHandler (nextAuto);newEventHandler(nextAuto);    按钮 previous = new 按钮 ();Buttonprevious =newButton(); automation.Controls.Add(previous); automation.Controls.Add(previous); previous.Text = previous.Text = " Previous" ; ;"Previous";   ;    previous.Location = previous.Location = new Point (160, 215);newPoint(160, 215);   ;     previous.Click + = previous.Click += new EventHandler (previousAuto);newEventHandler(previousAuto);    按钮 newauto = new 按钮 ();Buttonnewauto =newButton(); automation.Controls.Add(newauto); automation.Controls.Add(newauto); newauto.Text = newauto.Text = ""New" ; ;"New";   ;    newauto.Location = newauto.Location = new Point (240, 215);newPoint(240, 215);   ;     newauto.Click + = newauto.Click += new EventHandler (newAuto);newEventHandler(newAuto);    autoconnects.ReadXml( autoconnects.ReadXml( @" C:\用户\ J Du Plessis \Documents\Visual Studio 2010 \Projects\IRC Client \Auto.xml" );@"C:\Users\J Du Plessis\Documents\Visual Studio 2010\Projects\IRC Client\Auto.xml"); DataGridView VIEWautoconnections = new DataGridView ();DataGridViewVIEWautoconnections =newDataGridView(); automation.Controls.Add(VIEWautoconnections); automation.Controls.Add(VIEWautoconnections); VIEWautoconnections.DataSource = autoconnects; VIEWautoconnections.DataSource = autoconnects; // BindingNavigator nav = new BindingNavigator(true); BindingSource autoconnectsbinding = new BindingSource ();BindingSourceautoconnectsbinding =newBindingSource(); autoconnectsbinding.DataSource = autoconnects; autoconnectsbinding.DataSource = autoconnects; VIEWautoconnections.Location = VIEWautoconnections.Location = new Point (0, 12);newPoint(0, 12);   ;     VIEWautoconnections.Size = VIEWautoconnections.Size = new 尺寸 (300, 190);newSize(300, 190);   ;     VIEWautoconnections.DataMember = VIEWautoconnections.DataMember = "自动化" ; ;"automation";   ;    // nav.BindingSource = autoconnectsbinding; // automation.Controls.Add(nav); // nav.Location = new Point(0, 0); //将TextBox控件绑定到DataTable列中数据集。 DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn ();DataGridViewComboBoxColumncol =newDataGridViewComboBoxColumn(); col.Name = col.Name = "网络" ;"Network";   ;  / *的DataGridViewColumn COL1 =新的DataGridViewColumn(); col1.Name ="昵称"; col1.Name = "Nickname"; DataGridViewColumn col2 = new DataGridViewColumn(); DataGridViewColumn col2 = new DataGridViewColumn(); col2.Name =" e-mail"; col2.Name = "e-mail"; 的DataGridViewColumn COL3 =新的DataGridViewColumn(); DataGridViewColumn col3 = new DataGridViewColumn();            col3.Name =" nickserv"; col3.Name = "nickserv";   ;    VIEWautoconnections.Columns.Add(col); VIEWautoconnections.Columns.Add(col); VIEWautoconnections.Columns.Add(col1); VIEWautoconnections.Columns.Add(col1); VIEWautoconnections.Columns.Add(col2); VIEWautoconnections.Columns.Add(col2); VIEWautoconnections.Columns.Add(col3); VIEWautoconnections.Columns.Add(col3); VIEWautoconnections.AutoGenerateColumns = true; * / VIEWautoconnections.AutoGenerateColumns = true;*/ VIEWautoconnections.Columns.Add(col); VIEWautoconnections.Columns.Add(col); foreach ( DataTable dataTable autoconnects.Tables)foreach(DataTabledataTableinautoconnects.Tables) { { foreach ( DataRow row in dataTable.Rows)foreach(DataRowrowindataTable.Rows)   ;     { { col.Items.Add(row [2] .ToString()); col.Items.Add(row[2].ToString()); ;                         &NBSP ;  // MessageBox.Show(row [0] .ToString());   ;          //表单服务器=新客户端("irc.atrum.org"," string realname"," string username",dataTable ..,string email)                } } } } / * Label networklabel = new Label(); networklabel.Text =" Network"; networklabel.Text = "Network"; networklabel.Location = new Point(10,10); networklabel.Location = new Point(10, 10); automation.Controls.Add(networklabel); automation.Controls.Add(networklabel); TextBox network = new TextBox(); TextBox network = new TextBox(); network.DataBindings.Add(" Text",autoconnects.Tables [" network"]," network"); network.DataBindings.Add("Text", autoconnects.Tables["network"], "network");       network.Location = new Point(100,10); network.Location = new Point(100, 10); automation.Controls.Add(network); automation.Controls.Add(network); Label nicknamelabel = new Label(); Label nicknamelabel = new Label(); nicknamelabel.Location = new Point(10, 40); nicknamelabel.Location = new Point(10, 40); nicknamelabel.Text = "Nickname"; nicknamelabel.Text = "Nickname"; automation.Controls.Add(nicknamelabel); automation.Controls.Add(nicknamelabel); TextBox nickname = new TextBox(); TextBox nickname = new TextBox(); nickname.DataBindings.Add("Text", autoconnects.Tables["nickname"], "nickname"); nickname.DataBindings.Add("Text", autoconnects.Tables["nickname"], "nickname"); nickname.Location = new Point(100, 40); nickname.Location = new Point(100, 40); automation.Controls.Add(nickname); automation.Controls.Add(nickname); Label nickservLabel = new Label(); Label nickservLabel = new Label(); nickservLabel.Location = new Point(10, 70); nickservLabel.Location = new Point(10, 70); nickservLabel.Text = "NickServ password"; nickservLabel.Text = "NickServ password"; automation.Controls.Add(nickservLabel); automation.Controls.Add(nickservLabel); TextBox nickserv = new TextBox(); TextBox nickserv = new TextBox(); nickserv.DataBindings.Add("Text", autoconnects.Tables["nickserv"], "nickserv"); nickserv.DataBindings.Add("Text", autoconnects.Tables["nickserv"], "nickserv"); automation.Controls.Add(nickserv); automation.Controls.Add(nickserv); nickserv.Location = new Point(100, 70);*/ nickserv.Location = new Point(100, 70);*/ // Specify the CurrencyManager for the DataTable. /*myCurrencyManager = (CurrencyManager)this.BindingContext[autoconnectsbinding]; // Set the initial Position of the control. // Set the initial Position of the control. myCurrencyManager.Position = 0; myCurrencyManager.Position = 0; /*DataSet serverautoconnects = new DataSet("Servers"); /*DataSet serverautoconnects = new DataSet("Servers"); serverautoconnects = xmlDatadoc.DataSet; serverautoconnects = xmlDatadoc.DataSet; XmlDataDocument servers = new XmlDataDocument(); XmlDataDocument servers = new XmlDataDocument(); xmlDatadoc.DataSet.ReadXml("Servers.xml"); xmlDatadoc.DataSet.ReadXml("Servers.xml"); DataSet NetworkAutoconnects = new DataSet("Auto Connects"); DataSet NetworkAutoconnects = new DataSet("Auto Connects"); NetworkAutoconnects = xmlDatadoc.DataSet;*/ NetworkAutoconnects = xmlDatadoc.DataSet;*/ this.Controls.Add(automation); automation.Width = 350; automation.Width = 350; automation.Height = 250; automation.Height = 250; automation.Location = automation.Location =newPoint(123, 12); automation.Visible = automation.Visible =true; VIEWautoconnections.AutoGenerateColumns = VIEWautoconnections.AutoGenerateColumns =true; //VIEWautoconnections.Columns.Remove("network"); //string firstColumn = VIEWautoconnections.Columns[1].ToString(); //MessageBox.Show(firstColumn); } }推荐答案Hello,Even thou you indicated a problem accessing a DataGridView Column by numeric index of the column in the DataGridView unless manually added you have not explain exactly what happens when columns are created in the IDE DataGridView designer. Can you indicate what happens?Even thou you indicated a problem accessing a DataGridView Column by numeric index of the column in the DataGridView unless manually added you have not explain exactly what happens when columns are created in the IDE DataGridView designer. Can you indicate what happens?Secondly, I only see one place where you ae accessing a column by numeric index and that line is commented out unless I am missing something. Best to keep your code to the point, only show code that is specific to the issue at hand.Secondly, I only see one place where you ae accessing a column by numeric index and that line is commented out unless I am missing something. Best to keep your code to the point, only show code that is specific to the issue at hand. 这篇关于dbgrid列索引丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 10:13