用的时Acces表,表中有两个字段 username 和 password 均为string型的。
怎样在DataGrid上点击《编辑》时,取出当前行各列的值,并赋给不同的两个全局变量name 和 pass呢?(目的时:点击编辑时取出该行各列的值,在更新DataGrid时当作更新条件用)。
非常感谢各位的指点!(最好提供代码)。

解决方案 »

  1.   


    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string name=e.Item.Cells[0].Text;//Cells[在DataGrid1中的列索引
    string pass=e.Item.Cells[1].Text;
    }
      

  2.   

    private void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
       this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
        重新绑定数据;
    }private void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
      string name=e.Item.Cells[0].Text;//0表示第一列;
      string pass=e.Item.Cells[1].Text;
      ...............................
      重新绑定数据;
    }
      

  3.   

    下面是我的代码:public string name="",pas="";public void BindGrid()
    {
       OleDbConnection myConnection = new OleDbConnection(ConnectionString);
       OleDbDataAdapter myCommand = new OleDbDataAdapter(SelectCommand, myConnection);   DataSet ds = new DataSet();
       myCommand.Fill(ds,"pic_user");
       DataView dv = ds.Tables["pic_user"].DefaultView; 
       dv.Sort = SortString;
       DataGrid1.DataSource = dv;
       DataGrid1.DataBind();
    }
    //-------------------------------------------------------------------
    public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       DataGrid1.Columns[0].HeaderText="处于编辑状态";
       DataGrid1.EditItemIndex=e.Item.ItemIndex;
       BindGrid();
       name=e.Item.Cells[2].Text;
       pas=e.Item.Cells[3].Text;
       Response.Write("  name:   "+name+"   pas:   "+pas);
    }
    怎么没有取出数据啊? 打印结果是: name: pas: 
      

  4.   

    用FindControl代码如下:name=((TextBox)e.Item.FindControl("usname")).Text;
    pas=((TextBox)e.Item.FindControl("uspass")).Text;点击编辑后出错,说是《未将对象引用设置到对象的实例。》
    行 45:  DataGrid1.Columns[0].HeaderText="处于编辑状态";
    行 46:  DataGrid1.EditItemIndex=e.Item.ItemIndex;
    行 47:  name=((TextBox)e.Item.FindControl("usname")).Text;//这是出错行
    行 48:  pas=((TextBox)e.Item.FindControl("uspass")).Text;
    行 49:  BindGrid();
      

  5.   

    模板列edititemtemplate中有textbox吗
    参考
    http://blog.csdn.net/lovecherry/archive/2005/02/25/301441.aspx
      

  6.   

    我同样有过这样的情况出现过
    我的做法是再加两个同样的字段,把它们隐藏,而且为readonly
    取的时候编辑到的项是取不到值的
    所以取就取隐藏的那两个字段就行了
      

  7.   

    是啊,加入名为usname的TextBox了吗?
      

  8.   

    在模板列edititemtemplate中当然有textbox,而且它的 id 分别为 id="usname", id="uspass",若没有textbox对象和它的id 怎么能用FindControl来取出它的值呢。所有的东东我都检查过了,单就是取不出它的值。
    用FindControl时出现那种莫名其妙的错误,而用Cells时取不出值来,不知怎么弄,真头疼!
    请大侠们帮帮忙!
      

  9.   

    断点检查是不是找到了这个控件
    检查前台控件是不是runat=server了
      

  10.   

    是runat=server 啊<EditItemTemplate>
    <asp:TextBox id="usname" runat="server" Width="200" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'></asp:TextBox>
    </EditItemTemplate><EditItemTemplate>
    <asp:TextBox id="uspass" runat="server" Width="100" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:TextBox>
    </EditItemTemplate>
      

  11.   

    DataGrid1.EditItemIndex=e.Item.ItemIndex;
    行 47:  name=((TextBox)e.Item.FindControl("usname")).Text;//这是出错行
    行 48:  pas=((TextBox)e.Item.FindControl("uspass")).Text;
    行 49:  BindGrid();
    把49行移到47前面再试试看
      

  12.   

    把datagrid在表现层的代码贴出来看一下。
      

  13.   

    string username=((TextBox)e.Item.Cells[1].Controls[0]).Text.Trim();
    这样肯定可以
      

  14.   

    <P><asp:datagrid id=DataGrid1 runat="server"  OnEditCommand="DataGrid1_EditCommand" OnCancelCommand="DataGrid1_CancelCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnDeleteCommand="DataGrid1_DeleteCommand" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnSortCommand="DataGrid1_SortCommand" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="0" Width="397px" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True" PageSize="7">
    <FooterStyle Wrap="False" ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <SelectedItemStyle Font-Bold="True" Wrap="False" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <EditItemStyle Wrap="False"></EditItemStyle>
    <AlternatingItemStyle Wrap="False" BackColor="WhiteSmoke"></AlternatingItemStyle>
    <ItemStyle Wrap="False" ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" Wrap="False" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <Columns>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False" BorderColor="Silver" BackColor="White"></ItemStyle>
    <FooterStyle Wrap="False"></FooterStyle>
    </asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    <FooterStyle Wrap="False"></FooterStyle>
    </asp:ButtonColumn>
    <asp:TemplateColumn SortExpression="UserName" HeaderText="UserName">
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox id="usname" runat="server" Width="200" Text='<%# DataBinder.Eval(Container, "DataItem.UserName") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn SortExpression="Password" HeaderText="Password">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    <ItemTemplate>
    <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterStyle Wrap="False"></FooterStyle>
    <EditItemTemplate>
    <asp:TextBox id="uspass" runat="server" Width="100" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Wrap="False" Mode="NumericPages"></PagerStyle>
    </asp:datagrid></P>
      

  15.   

    用下面的两种代码的任何一种时出现《指定的转换无效》的错误。
    name=((TextBox)e.Item.Cells[2].Controls[0]).Text.Trim();
    pas= ((TextBox)e.Item.Cells[3].Controls[0]).Text.Trim();name=((TextBox)e.Item.Cells[2].Controls[0]).Text;
    pas= ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    ------------------------------------------------------------------用下面的代码时出现《为将对象引用设置到对象的事例》的错误name=((TextBox)e.Item.FindControl("usname")).Text;
    pas= ((TextBox)e.Item.FindControl("uspass")).Text;真搞不清到底是那里的问题,请大侠们帮帮忙!
      

  16.   

    name=((TextBox)e.Item.Cells[2].Controls[0]).Text.Trim();
    pas= ((TextBox)e.Item.Cells[3].Controls[0]).Text.Trim();
    上面是在点更新的时候取值用的点编辑时应该用
    name=((Label)e.Item.Cells[2].Controls[0]).Text.Trim();
    pas= ((Label)e.Item.Cells[3].Controls[0]).Text.Trim();
    这下应该可以了吧
      

  17.   

    把所有的:<%# DataBinder.Eval(Container, "DataItem.Password") %>
    换成:<%# DataBinder.Eval(Container.DataItem, "Password") %>
    用户名的也是相应的更改一下,试试行不行
      

  18.   

    to: xxqqpp(想要变成鱼): 按你说的做了,还是不行,出现《指定的转换无效》的错误  to: tomtown530(梦想一定会实现!) : 按你说的改了还是出现《指定的转换无效》的错误。哪位高手帮我看看,请留给你的MSN 或 Email 我将文件发给你。
    我的MSN:[email protected] (我一直在线)。
      

  19.   

    string name=e.Item.Cells[1].Text.ToString();
    这个可行,
      

  20.   

    public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       DataGrid1.Columns[0].HeaderText="处于编辑状态";
       DataGrid1.EditItemIndex=e.Item.ItemIndex;   
       name=e.Item.Cells[2].Text;
       pas=e.Item.Cells[3].Text;
       Response.Write("  name:   "+name+"   pas:   "+pas);
       BindGrid();
    }这样试试
      

  21.   

    这个代码取的值是空的,没有取到值。
    name=e.Item.Cells[2].Text;
    pas=e.Item.Cells[3].Text;
      

  22.   

    private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string sql="update documents set subject=@subject,author=@author where id=@id";
    SqlCommand objComm = new SqlCommand(sql,objConn);
    objComm.Parameters.Add("@subject",SqlDbType.VarChar,20);
    objComm.Parameters.Add("@author",SqlDbType.VarChar,20);
    objComm.Parameters.Add("@id",SqlDbType.Int);
    TextBox t1=(TextBox)e.Item.Cells[1].Controls[0];
    TextBox t2=(TextBox)e.Item.Cells[2].Controls[0];
    objComm.Parameters["@subject"].Value=t1.Text;
    objComm.Parameters["@author"].Value=t2.Text;
    objComm.Parameters["@id"].Value=e.Item.Cells[0].Text;
    objConn.Open();
    objComm.ExecuteNonQuery();
    objConn.Close();
    DataGrid1.EditItemIndex=-1;
    bindgrid();
    }
      

  23.   

    终于自己解决了! 用的是Acces表,表名:pic_info2003.mdb, 表中的字段有:ID (key field),UserName 和 Password 。整个文件后台代码如下:
    --------------------------------------
    <%@ page language="C#" Debug="true" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %><Script Language="C#" Runat="Server">
    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\inetpub\\wwwroot\\PicManager\\DataBase\\pic_info2003.mdb;User ID=Admin;Password=;";
    string SelectCommand = "SELECT * from pic_user";public string SortString="",User="",Pass="";
    public int id=0;//-----------------------------------------------------------------------
    public void BindGrid()
    {
       OleDbConnection myConnection = new OleDbConnection(ConnectionString);
       OleDbDataAdapter myCommand = new OleDbDataAdapter(SelectCommand, myConnection);   DataSet ds = new DataSet();
       myCommand.Fill(ds,"pic_user");
       DataView dv = ds.Tables["pic_user"].DefaultView; 
       dv.Sort = SortString;
       DataGrid1.DataSource = dv;
       DataGrid1.DataBind();
    }
    //-----------------------------------------------------------------------
    public void Page_Load(object sender, System.EventArgs e)
    {
       if (!IsPostBack)
          BindGrid();
    }
    //-----------------------------------------------------------------------
    public void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex=e.NewPageIndex;
        BindGrid();
    }
    //-----------------------------------------------------------------------
    public void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
       SortString = e.SortExpression.ToString();
       BindGrid();
    }
    //-----------------------------------------------------------------------
    public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       //Response.Write("<script language=javascript>window.confirm('确实要修改吗?');</"+"script>");
       DataGrid1.Columns[0].HeaderText="处于编辑状态";
       DataGrid1.EditItemIndex=e.Item.ItemIndex;
       BindGrid();
    }
    //-----------------------------------------------------------------------
    public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       DataGrid1.Columns[0].HeaderText="已取消";
       DataGrid1.EditItemIndex=-1;
       BindGrid();
    }
    //-----------------------------------------------------------------------
    public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       id=int.Parse(e.Item.Cells[2].Text);
       User=((TextBox)e.Item.Cells[3].Controls[0]).Text;
       Pass=((TextBox)e.Item.Cells[4].Controls[0]).Text;
       Response.Write(id.ToString()+"  user:  "+User+"pass:  "+Pass);

       string UpdateString="UPDATE pic_user SET [UserName]='"+User+"',[Password]='"+Pass+"' WHERE [ID]="+id;   OleDbConnection myConnection=new OleDbConnection(ConnectionString);
       OleDbCommand MyCommand=new OleDbCommand(UpdateString,myConnection);   myConnection.Open();
       MyCommand.ExecuteNonQuery();
       myConnection.Close();
       DataGrid1.Columns[0].HeaderText="已更新";
       DataGrid1.EditItemIndex = -1;
       BindGrid();
    }
    //-----------------------------------------------------------------------
    public void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       id=int.Parse(e.Item.Cells[2].Text);
       string DeleteString="DELETE FROM pic_user WHERE [ID]="+id;

       OleDbConnection myConnection=new OleDbConnection(ConnectionString);
       OleDbCommand MyCommand=new OleDbCommand(DeleteString,myConnection);   myConnection.Open();
       MyCommand.ExecuteNonQuery();
       myConnection.Close();
       DataGrid1.Columns[1].HeaderText="已删除";
       DataGrid1.EditItemIndex = -1;
       BindGrid();
    }
    //-----------------------------------------------------------------------
    public void Button1_Click(object sender, System.EventArgs e)
    {
       string username=TextBox1.Text;
       string password=TextBox2.Text;
       OleDbConnection myConnection=new OleDbConnection(ConnectionString);
       string InsertString="INSERT INTO pic_user([UserName],[Password]) VALUES ('"+username+"' ,'"+ password+"')";
       OleDbCommand MyCommand=new OleDbCommand(InsertString,myConnection);
    //Response.Write(InsertString);
       myConnection.Open();
       MyCommand.ExecuteNonQuery();
       myConnection.Close();
       BindGrid();
       TextBox1.Text="";
       TextBox2.Text="";

    </Script>
      

  24.   

    整个文件前台代码如下:
    <HTML>
       <HEAD>
          <title>users</title>
          <meta content="False" name="vs_snapToGrid">
          <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
          <meta content="C#" name="CODE_LANGUAGE">
          <meta content="JavaScript" name="vs_defaultClientScript">
          <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
       </HEAD>
          <body dir="ltr" MS_POSITIONING="GridLayout">
             <form id="Form1" method="post" runat="server">
       <TABLE id="Table1" style="Z-INDEX: 102; LEFT: 235px; WIDTH: 567px; POSITION: absolute; TOP: 38px" height="369" cellSpacing="0" cellPadding="0" width="567" background="/PicManager/tabback2.gif" border="0" align="center">
       <TR>
          <TD style="HEIGHT: 39px" colSpan="3">
          <DL>
           <DD>
              <H2 id="H21" style="VERTICAL-ALIGN: baseline; COLOR: white; FONT-STYLE: normal; FONT-FAMILY: Arial; TEXT-ALIGN: center"
    runat="server">Modifaction Users Table</H2>
           </DD>
        </DL>
          </TD>
       </TR>
    <FONT face="宋体">
    <TR>
       <TD style="WIDTH: 253px; HEIGHT: 55px" noWrap colSpan="1" rowSpan="1">
       <P><FONT face="宋体">&nbsp;UserName:
    <asp:textbox id="TextBox1" runat="server"></asp:textbox></FONT></P>
    <P><FONT face="宋体">&nbsp;</P>
       </FONT></TD>
       <TD style="WIDTH: 239px; HEIGHT: 55px">
       <P><FONT face="宋体">Password:
       <asp:textbox id="TextBox2" runat="server"></asp:textbox></FONT></P>
       <P><FONT face="宋体">&nbsp;</P>
       </FONT></TD>
       <TD style="HEIGHT: 55px"><FONT face="宋体">
       <P><asp:button id="Button1" OnClick="Button1_Click" runat="server" Text="Add..."></asp:button></P>
       <P>&nbsp;</P>
       </FONT>
       </TD>
       </TR>
       <TR>
       <TD vAlign="top" align="center" colSpan="3">
       <P><asp:datagrid id="DataGrid1" DtaKeyField="ID" runat="server"  OnEditCommand="DataGrid1_EditCommand" OnCancelCommand="DataGrid1_CancelCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnDeleteCommand="DataGrid1_DeleteCommand" OnPageIndexChanged="DataGrid1_PageIndexChanged" OnSortCommand="DataGrid1_SortCommand" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="0" Width="397px" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True" PageSize="7">
       <FooterStyle Wrap="False" ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
       <SelectedItemStyle Font-Bold="True" Wrap="False" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
       <EditItemStyle Wrap="False"></EditItemStyle>
       <AlternatingItemStyle Wrap="False" BackColor="Gainsboro"></AlternatingItemStyle>
       <ItemStyle Wrap="False" ForeColor="#330099" BackColor="White"></ItemStyle>
       <HeaderStyle Font-Bold="True" Wrap="False" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
       <Columns>
       <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
       <HeaderStyle Wrap="False"></HeaderStyle>
       <ItemStyle Wrap="False" BorderColor="Silver" BackColor="White"></ItemStyle>
       <FooterStyle Wrap="False"></FooterStyle>
    </asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete">
    <HeaderStyle Wrap="False"></HeaderStyle>
    <ItemStyle Wrap="False"></ItemStyle>
    <FooterStyle Wrap="False"></FooterStyle>
    </asp:ButtonColumn>
    <asp:BoundColumn DataField="ID" SortExpression="ID" HeaderText="ID" ReadOnly="True"></asp:BoundColumn>
    <asp:BoundColumn DataField="UserName" SortExpression="UserName" HeaderText="UserName"></asp:BoundColumn>
    <asp:BoundColumn DataField="Password" SortExpression="Password" HeaderText="Password"></asp:BoundColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Wrap="False" Mode="NumericPages"></PagerStyle>
    </asp:datagrid></P>
    <P><FONT face="宋体"></FONT>&nbsp;</P>
    </TD>
    </TR>
    </TABLE>
    </FONT></form>
    </body>
    </HTML>