Gridview破了 - 什么都没改变

时间:2015-10-23 08:58:03

标签: c# asp.net gridview

我在ASP.Net中创建一个应用程序,团队负责人可以针对特定项目每小时编辑目标。我今天计划将这个项目介绍给多个人,我调试了项目以便完成并练习我的演讲。

然而,出现了一个问题。我从Gridview中检索要插入数据库的值的代码被破坏了。

我收到的错误消息是:

  

类型' System.ArgumentOutOfRangeException'的例外情况发生在   System.Web.dll但未在用户代码中处理

     

附加信息:指定的参数超出了有效值的范围。

我用来检索值的代码是:

 string contractName = gvShowData.DataKeys[e.RowIndex].Value.ToString();
 GridViewRow row = (GridViewRow)gvShowData.Rows[e.RowIndex];
 TextBox txtContractName = (TextBox)row.Cells[0].FindControl("Contract");
 string txtCName = ((TextBox)row.Cells[1].Controls[0]).Text;
 string txtModel = ((TextBox)row.Cells[2].Controls[0]).Text;
 string txtProcess = ((TextBox)row.Cells[3].Controls[0]).Text;
 string txtH0 = ((TextBox)row.Cells[4].Controls[0]).Text;
 string txtH1 = ((TextBox)row.Cells[5].Controls[0]).Text;
 string txtH2 = ((TextBox)row.Cells[6].Controls[0]).Text;
 string txtH3 = ((TextBox)row.Cells[7].Controls[0]).Text;
 string txtH4 = ((TextBox)row.Cells[8].Controls[0]).Text;
 string txtH5 = ((TextBox)row.Cells[9].Controls[0]).Text;
 string txtH6 = ((TextBox)row.Cells[10].Controls[0]).Text;
 string txtH7 = ((TextBox)row.Cells[11].Controls[0]).Text;
 string txtH8 = ((TextBox)row.Cells[12].Controls[0]).Text;
 string txtH9 = ((TextBox)row.Cells[13].Controls[0]).Text;
 string txtH10 = ((TextBox)row.Cells[14].Controls[0]).Text;
 string txtH11 = ((TextBox)row.Cells[15].Controls[0]).Text;
 string txtH12 = ((TextBox)row.Cells[16].Controls[0]).Text;
 string txtH13 = ((TextBox)row.Cells[17].Controls[0]).Text;
 string txtH14 = ((TextBox)row.Cells[18].Controls[0]).Text;
 string txtH15 = ((TextBox)row.Cells[19].Controls[0]).Text;
 string txtH16 = ((TextBox)row.Cells[20].Controls[0]).Text;
 string txtH17 = ((TextBox)row.Cells[21].Controls[0]).Text;
 string txtH18 = ((TextBox)row.Cells[22].Controls[0]).Text;
 string txtH19 = ((TextBox)row.Cells[23].Controls[0]).Text;
 string txtH20 = ((TextBox)row.Cells[24].Controls[0]).Text;
 string txtH21 = ((TextBox)row.Cells[25].Controls[0]).Text;
 string txtH22 = ((TextBox)row.Cells[26].Controls[0]).Text;
 string txtH23 = ((TextBox)row.Cells[27].Controls[0]).Text;

整个Update语句(删除了数据库连接字符串和表名)。

SqlConnection con = new SqlConnection(constring);

string contractName = gvShowData.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = (GridViewRow)gvShowData.Rows[e.RowIndex];
TextBox txtContractName = (TextBox)row.FindControl("Contract");
//string txtCName = row.Cells[1].Text;
//string txtModel = ((TextBox)row.Cells[2].Controls[0]).Text;
//string txtProcess = ((TextBox)row.Cells[3].Controls[0]).Text;
//string txtH0 = ((TextBox)row.Cells[4].Controls[0]).Text;
//string txtH1 = ((TextBox)row.Cells[5].Controls[0]).Text;
//string txtH2 = ((TextBox)row.Cells[6].Controls[0]).Text;
//string txtH3 = ((TextBox)row.Cells[7].Controls[0]).Text;
//string txtH4 = ((TextBox)row.Cells[8].Controls[0]).Text;
//string txtH5 = ((TextBox)row.Cells[9].Controls[0]).Text;
//string txtH6 = ((TextBox)row.Cells[10].Controls[0]).Text;
//string txtH7 = ((TextBox)row.Cells[11].Controls[0]).Text;
//string txtH8 = ((TextBox)row.Cells[12].Controls[0]).Text;
//string txtH9 = ((TextBox)row.Cells[13].Controls[0]).Text;
//string txtH10 = ((TextBox)row.Cells[14].Controls[0]).Text;
//string txtH11 = ((TextBox)row.Cells[15].Controls[0]).Text;
//string txtH12 = ((TextBox)row.Cells[16].Controls[0]).Text;
//string txtH13 = ((TextBox)row.Cells[17].Controls[0]).Text;
//string txtH14 = ((TextBox)row.Cells[18].Controls[0]).Text;
//string txtH15 = ((TextBox)row.Cells[19].Controls[0]).Text;
//string txtH16 = ((TextBox)row.Cells[20].Controls[0]).Text;
//string txtH17 = ((TextBox)row.Cells[21].Controls[0]).Text;
//string txtH18 = ((TextBox)row.Cells[22].Controls[0]).Text;
//string txtH19 = ((TextBox)row.Cells[23].Controls[0]).Text;
//string txtH20 = ((TextBox)row.Cells[24].Controls[0]).Text;
//string txtH21 = ((TextBox)row.Cells[25].Controls[0]).Text;
//string txtH22 = ((TextBox)row.Cells[26].Controls[0]).Text;
//string txtH23 = ((TextBox)row.Cells[27].Controls[0]).Text;

var list = new List<string>();

foreach(var cell in row.Cells)
{
    list.Add((cell.Controls[0] as TextBox).Text);
}


gvShowData.EditIndex = -1;
con.Open();

DateTime time = DateTime.Now;
string format = "yyyy-MM-dd HH:mm:ss.fff";

string commandString = "INSERT INTO dms (Contract, Line, Process, H0,H1,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,H12,H13,H14,H15,H16,H17,H18,H19,H20,H21,H22,H23,timestamp,username) VALUES('" + txtCName + "','" + txtModel + "','" + txtProcess + "','" + txtH0 + "','" + txtH1 + "','" + txtH2 + "','" + txtH3 + "','" + txtH4 + "','" + txtH5 + "','" + txtH6 + "','" + txtH7 + "','" + txtH8 + "','" + txtH9 + "','" + txtH10 + "','" + txtH11 + "','" + txtH12 + "','" + txtH13 + "','" + txtH14 + "','" + txtH15 + "','" + txtH16 + "','" + txtH17 + "','" + txtH18 + "','" + txtH19 + "','" + txtH20 + "','" + txtH21 + "','" + txtH22 + "','" + txtH23 + "','" + System.DateTime.Now.ToString(format) + "','" + User.Identity.Name.ToString() + "')";

SqlCommand com = new SqlCommand(commandString, con);
com.ExecuteNonQuery();
con.Close();
BindGridView();

2 个答案:

答案 0 :(得分:2)

你可以重构一下:

 string txtCName = ((TextBox)row.Cells[1].Controls[0]).Text;
 string txtModel = ((TextBox)row.Cells[2].Controls[0]).Text;
 string txtProcess = ((TextBox)row.Cells[3].Controls[0]).Text;
 string txtH0 = ((TextBox)row.Cells[4].Controls[0]).Text;
 string txtH1 = ((TextBox)row.Cells[5].Controls[0]).Text;
 string txtH2 = ((TextBox)row.Cells[6].Controls[0]).Text;
 string txtH3 = ((TextBox)row.Cells[7].Controls[0]).Text;
 string txtH4 = ((TextBox)row.Cells[8].Controls[0]).Text;
 string txtH5 = ((TextBox)row.Cells[9].Controls[0]).Text;
 string txtH6 = ((TextBox)row.Cells[10].Controls[0]).Text;
 string txtH7 = ((TextBox)row.Cells[11].Controls[0]).Text;
 string txtH8 = ((TextBox)row.Cells[12].Controls[0]).Text;
 string txtH9 = ((TextBox)row.Cells[13].Controls[0]).Text;
 string txtH10 = ((TextBox)row.Cells[14].Controls[0]).Text;
 string txtH11 = ((TextBox)row.Cells[15].Controls[0]).Text;
 string txtH12 = ((TextBox)row.Cells[16].Controls[0]).Text;
 string txtH13 = ((TextBox)row.Cells[17].Controls[0]).Text;
 string txtH14 = ((TextBox)row.Cells[18].Controls[0]).Text;
 string txtH15 = ((TextBox)row.Cells[19].Controls[0]).Text;
 string txtH16 = ((TextBox)row.Cells[20].Controls[0]).Text;
 string txtH17 = ((TextBox)row.Cells[21].Controls[0]).Text;
 string txtH18 = ((TextBox)row.Cells[22].Controls[0]).Text;
 string txtH19 = ((TextBox)row.Cells[23].Controls[0]).Text;
 string txtH20 = ((TextBox)row.Cells[24].Controls[0]).Text;
 string txtH21 = ((TextBox)row.Cells[25].Controls[0]).Text;
 string txtH22 = ((TextBox)row.Cells[26].Controls[0]).Text;
 string txtH23 = ((TextBox)row.Cells[27].Controls[0]).Text;

阅读:

var list = new List<string>();

foreach(TableCell cell in row.Cells)
   list.Add((cell.Controls[0] as TextBox).Text);

然后如何单独检索值会稍微改变一下。但是,由于您的变量名称似乎遵循严格的顺序,我不会发现通过索引获取列表元素是一个问题。

例如,myControl.Text = txtH0;代替myControl.Text = list[0];现在TextBox如何获得&#39; 0&#39;是你的,它通常是一个循环的一部分。

有些人可能会说这并不能回答这个问题,而是代码审查 - 但它 也能解决问题 - 现在也不例外。

注意:请注意您的控制肯定存在并且肯定是{{1}}

答案 1 :(得分:0)

我通过@JayMee回答来解决这个问题。

更新的新代码是:

        SqlConnection con = new SqlConnection(constring);
        string contractName = gvShowData.DataKeys[e.RowIndex].Value.ToString();
        GridViewRow row = (GridViewRow)gvShowData.Rows[e.RowIndex];
        TextBox txtContractName = (TextBox)row.FindControl("Contract");
        string txtCName = row.Cells[1].Text.ToString();
        string txtModel = row.Cells[2].Text.ToString();
        string txtProcess = row.Cells[3].Text.ToString();

        var list = new List<string>();

        for(int cell = 4; cell < 28; cell++)
        {
            list.Add((row.Cells[cell].Controls[0] as TextBox).Text.ToString());
        }

        gvShowData.EditIndex = -1;
        con.Open();

        DateTime time = DateTime.Now;
        string format = "yyyy-MM-dd HH:mm:ss.fff";

        string commandString = "INSERT INTO dms (Contract, Line, Process, H0,H1,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,H12,H13,H14,H15,H16,H17,H18,H19,H20,H21,H22,H23,timestamp,username) VALUES('" + txtCName + "','" + txtModel + "','" + txtProcess + "','" + list[0] + "','" + list[1] + "','" + list[2] + "','" + list[3] + "','" + list[4] + "','" + list[5] + "','" + list[6] + "','" + list[7] + "','" + list[8] + "','" + list[9] + "','" + list[10] + "','" + list[11] + "','" + list[12] + "','" + list[13] + "','" + list[14] + "','" + list[15] + "','" + list[16] + "','" + list[17] + "','" + list[18] + "','" + list[19] + "','" + list[20] + "','" + list[21] + "','" + list[22] + "','" + list[23] + "','" + System.DateTime.Now.ToString(format) + "','" + User.Identity.Name.ToString() + "')";

        SqlCommand com = new SqlCommand(commandString, con);
        com.ExecuteNonQuery();
        con.Close();
        BindGridView();