private void Page_Load(object sender, System.EventArgs e)
{
myConnection=new SqlConnection("workstation id=CHENPENGCHENG;packet size=4096;user id=sa;password=85225926;data s" +
"ource=chenpengcheng;persist security info=False;initial catalog=毕业设计");
BindGrid();// 在此处放置用户代码以初始化页面
}
public void Data_edit(object sender,DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
}
    public void Data_cancer(object sender,DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex=-1;
    BindGrid();
     }
public void Data_update(object sender,DataGridCommandEventArgs E)
{
string update="update d141 set ip地址=@ip地址,工作状态=@工作状态,房间号=@房间号,连接交换机=@连接交换机,连接端口号=@连接端口号 where 端口号=@端口号";
SqlCommand myCommand=new SqlCommand(update,myConnection);
myCommand.Parameters.Add(new SqlParameter("@端口号",SqlDbType.Int,10));
myCommand.Parameters.Add(new SqlParameter("@ip地址",SqlDbType.Char,10));
myCommand.Parameters.Add(new SqlParameter("@工作状态",SqlDbType.Char,10));
myCommand.Parameters.Add(new SqlParameter("@房间号",SqlDbType.Char,10));
myCommand.Parameters.Add(new SqlParameter("@连接交换机",SqlDbType.Char,15));
myCommand.Parameters.Add(new SqlParameter("@连接端口号",SqlDbType.Char,15));
myCommand.Parameters["@端口号"].Value=1;
String[] cols={"@ip地址","@工作状态","@房间号","@连接交换机","@连接端口号"};
int numCols=E.Item.Cells.Count;
for(int i=1;i<numCols-1;i++)
{
String colvalue=((TextBox)E.Item.Cells[i+1].Controls[0]).Text;
myCommand.Parameters[cols[i-1]].Value=colvalue;
}
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
BindGrid();
   }

public void BindGrid()
{
SqlDataAdapter myCommand1=new SqlDataAdapter("select 端口号,ip地址,工作状态,房间号,连接交换机,连接端口号 from d141",myConnection);
//myCommand1.SelectCommand.Parameters.Add(new SqlParameter("@端口号",SqlDbType.Int,4));
DataSet ds=new DataSet();
myCommand1.Fill(ds,"d141");
DataGrid1.DataSource=ds.Tables["d141"].DefaultView;
DataGrid1.DataBind();
}
   为什么我始终不能更新,输入数据后不变,如果把String colvalue=((TextBox)E.Item.Cells[i+1].Controls中的i+1改为i的话,只要一点更新所有的值跟着第一个数据的值变,如果是i+1的话,点更新无用,为什么?望达人指点

解决方案 »

  1.   

    for(int i=1;i<numCols-1;i++)
    {
    String colvalue=((TextBox)E.Item.Cells[i].Controls[0]).Text;
    myCommand.Parameters[cols[i]].Value=colvalue;
    }
      

  2.   

    for(int i=1;i<numCols-1;i++)
    {
    String colvalue=((TextBox)E.Item.Cells[i].Controls[1]).Text;
             //E.Item.Cells[i].Controls[1]改成
    myCommand.Parameters[cols[i]].Value=colvalue;
    }
      

  3.   


    myCommand.Parameters[cols[i-1]].Value=colvalue;
      

  4.   

    for(int i=1;i<numCols;i++)
    {
    String colvalue=((TextBox)E.Item.Cells[i].Controls[0]).Text;
    myCommand.Parameters[cols[i]].Value=colvalue;
    }
      

  5.   

    如果把Controls[0]改为Controls[1]的话会说:指定的参数已超出有效值的范围。参数名: index;
    谁知道E.Item.Cells[i].Controls[0]返回的是什么值呢?
      

  6.   

    谁知道:E.Item.Cells[i].Controls[0]是什么意思?
      

  7.   

    你的BindGrid在每次Page_Load都调用?不觉得奇怪码?
    写在
    (!IsPostBack)
    {
         BindGrid();
    }