0
我在Windows窗体应用程序中编写代码以更改标题名称。我们如何更改datagridview标题名称?
DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
columnHeaderStyle.BackColor = Color.Beige;
columnHeaderStyle.Font = new Font("Bookman Old Style", 8, FontStyle.Bold);
dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
dataGridView1.Columns[0].HeaderText = "Item Code";
dataGridView1.Columns[1].HeaderText = "Item Name";
但是,当我运行此代码时显示一个错误。 “Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index
” 我该如何解决?
很明显,你的'dataGridView1'没有任何列。 –
检查数据绑定,并确定网格中有实际的列。此外,您应该在网格列的属性上设置此值,而不是在代码中(除非您正在动态生成) –
是的。没有专栏。我想创建一个空白的datagridview。我在其中插入数据。当我按下保存按钮时,数据将保存到数据库。那么我该如何创建它? –