我正在尝试编辑我的gridview标题标题,但遗憾的是它不起作用
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT policeid, password, email, nric, fullname, contact, address, location From LoginRegisterPolice where pending='pending'", conn);
da.Fill(ds);
GVVerify.DataSource = ds;
GVVerify.DataBind();
GVVerify.Columns[1].HeaderText = "Police ID ";
GVVerify.Columns[2].HeaderText = "Password";
GVVerify.Columns[3].HeaderText = "Email ";
GVVerify.Columns[4].HeaderText = "NRIC ";
GVVerify.Columns[5].HeaderText = "Full Name ";
GVVerify.Columns[6].HeaderText = "Contact ";
GVVerify.Columns[7].HeaderText = "Address ";
GVVerify.Columns[8].HeaderText = "Location ";
conn.Close();
这是我收到的错误
指数超出范围。必须是非负数且小于 集合。
我检查过我的列索引不是负数,也是正确的列号。我在gridview中添加了allowgenerateselectbutton
。因此,我用1-8而不是0开始。
答案 0 :(得分:3)
您可以通过在select SQL中使用列别名来完成此操作,无需从代码中执行此操作。
SELECT policeid as [Police ID] , password as [Password] ...............
答案 1 :(得分:1)
columns属性是一个基于零的数组。除非存在隐藏的第一列,否则从1开始。将索引重新编号为0-7而不是1-8。
http://msdn.microsoft.com/en-US/library/system.windows.forms.datagridview.columns(v=vs.80).aspx
GVVerify.Columns[0].HeaderText = "Police ID ";
GVVerify.Columns[1].HeaderText = "Password";
GVVerify.Columns[2].HeaderText = "Email ";
GVVerify.Columns[3].HeaderText = "NRIC ";
GVVerify.Columns[4].HeaderText = "Full Name ";
GVVerify.Columns[5].HeaderText = "Contact ";
GVVerify.Columns[6].HeaderText = "Address ";
GVVerify.Columns[7].HeaderText = "Location ";
答案 2 :(得分:0)
你必须尝试Index 0
或尝试不同的方法,如
GVVerify.Columns[0].HeaderText = "Police ID ";
------------------------------------
--------------------------------------
or
GVVerify.Columns["columnname"].HeaderText = "Police ID ";
------------------
-------------------