我在代码后面有 2 列我有这个:
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = cs.getConnection();
string query = "SELECT ID , NAME FROM PROFITCATEGORIES";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand command = new SqlCommand(query, myConnection);
using (SqlDataReader rdr = command.ExecuteReader())
{
GridViewCategory.DataSource = rdr;
GridViewCategory.DataBind();
GridViewCategory.Columns[0].HeaderText = "Header text"; // ERROR IS HERE
}
}
}
但这给了我一个错误:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
错误在这一行: GridViewCategory.Columns[0].HeaderText = "Header text";