GRIDVIEW中编辑更新的时候总是提示索引超出范围。必须为非负值并小于集合大小 5
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){/*创建一个数据库连接...
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
/*创建一个数据库连接对象*/
SqlConnection conn = new SqlConnection();
//设置数据库连接字符串属性值
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True";
string drugKey = GridView1.DataKeys[e.RowIndex].Value.ToString();
////取得文本框中的输入内容
string drugID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string drugName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string drugType = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string drugBuyPrice = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string drugCompany = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string drugFactory = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
string drugBuyCount = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim();
string drugBuyData = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim();
string sqlStr = "update BuyTable set DrugID='" + drugID + "',DrugName='" + drugName + "',DrugType='" + drugBuyPrice + "',DrugCompany='" + drugCompany + "',DrugFactory='" + drugFactory + "',DrugBuyCount='" + drugBuyCount + "',DrugBuyData='" + drugBuyData + "' where ID='" + drugKey + "'";
SqlCommand comm = new SqlCommand("sqlStr",conn);
conn.Open();
comm.ExecuteNonQuery();
GridView1.EditIndex = -1;
conn.Close();
BindGrid();
}
下面是我的数据库表 展开
{
/*创建一个数据库连接对象*/
SqlConnection conn = new SqlConnection();
//设置数据库连接字符串属性值
conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True";
string drugKey = GridView1.DataKeys[e.RowIndex].Value.ToString();
////取得文本框中的输入内容
string drugID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string drugName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string drugType = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string drugBuyPrice = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string drugCompany = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string drugFactory = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
string drugBuyCount = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim();
string drugBuyData = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim();
string sqlStr = "update BuyTable set DrugID='" + drugID + "',DrugName='" + drugName + "',DrugType='" + drugBuyPrice + "',DrugCompany='" + drugCompany + "',DrugFactory='" + drugFactory + "',DrugBuyCount='" + drugBuyCount + "',DrugBuyData='" + drugBuyData + "' where ID='" + drugKey + "'";
SqlCommand comm = new SqlCommand("sqlStr",conn);
conn.Open();
comm.ExecuteNonQuery();
GridView1.EditIndex = -1;
conn.Close();
BindGrid();
}
下面是我的数据库表 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询