我有一个gridview,当单击一个按钮时,它会从数据表中填充。
代码如下:
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("One", typeof(string)),
new DataColumn("Two", typeof(string)),
new DataColumn("Three",typeof(string)) });
string strConnectionString = ConfigurationManager.ConnectionStrings["---"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnectionString);
using (SqlCommand cmd = new SqlCommand("Select * FROM ---", conn))
{
conn.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dt.Rows.Add(reader["One"].ToString(), reader["Two"].ToString(), reader["Three"].ToString());
}
conn.Close();
}
GridView2.DataSource = dt;
GridView2.DataBind();
现在每当我尝试将DataKeyName添加到gridview时,都不会填充它。
我尝试手动设置并使用
GridView2.DataKeyNames = new string[] {"Key"};