想要实现的功能,在编辑行的时候自动把当前的用户名写入数据库 
目前我用response测试显示((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text,正确显示当前用户名, 
但是并没有写入到数据库 ,在点了编辑按钮的时候,对应的单元格数据也没变 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
        ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text = (string)Session["UserName"]; 
    } 同时想问下,如果我想把一个值给单元格,请问是在那个语句里面写protected void GridView1_RowUpdating里面写了好像不对

解决方案 »

  1.   

    你用FindControl方法找子控件吧..
    不要用((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text
      

  2.   

    更新完后记得重新绑定,把e.EditIndex设置为-1;再绑定 
      

  3.   

    请问如何重新绑定呢,谢谢了
    FindControl是如何找的呢
      

  4.   

    假如你的GridView中有一列模板列,即定义为<asp:TemplateField>的这种,里面有一个TextBox控件(也可能是其他控件)。那么你要取这个控件的值的话,就可以这样:
    ((TextBox)GridView1.Rows[i].Cells[0].FindControl('TextBox的ID')).Text.ToString()。置于重新绑定,是指你的更新已经插入到数据库了,那么你就要重新取一次数据绑定到GridView上,才能体现出数据的变化。。
      

  5.   

    我的这个不一样,是点击编辑后就将值绑定到了选中行的制定列4中,将相当于动态默认样,然后在点击更新的时候吧数据直接写入数据库完成记录的写入
    直接用的gridvew控件
    上次做出来次,但是代码忘记了,以前的文件给删除了
      

  6.   

    你的意思是不是点击编辑按钮的时候,就把当前编辑用户的ID存储到数据库中去??又或是显示在GridView中的TextBox中?不明白!~~
      

  7.   

    又或是显示在GridView中的TextBox中?
    在点击更新后直接就更新进去了
      

  8.   

    依据你说的,下面代码对你应该有用: 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                 try
                {
                    //GridViewRow row = GridView1.Rows[e.RowIndex];//((TextBox)(row.Controls[1].Controls[0])).Text; 
                    GridViewRow row = GridView1.Rows[GridView1.EditIndex];
                     //Label labObjScoreNum1 = e.Row.FindControl("labObjScoreNum1") as Label ; labObjScoreNum1.text
                     Label lab=row.Controls[14].FindControl("Label1") as Label ;                string id = lab.Text.ToString();                string 
    _xm = ((TextBox)(row.Controls[1].Controls[0])).Text.ToString();
                    string 
    _IDC = ((TextBox)(row.Controls[2].Controls[0])).Text.ToString();
                    string 
    _Area = ((TextBox)(row.Controls[3].Controls[0])).Text.ToString();
                    string 
    _Street = ((TextBox)(row.Controls[4].Controls[0])).Text.ToString();
                    string 
    _committee = ((TextBox)(row.Controls[5].Controls[0])).Text.ToString();
                    string 
    _address = ((TextBox)(row.Controls[6].Controls[0])).Text.ToString();
                    string 
    _telephone = ((TextBox)(row.Controls[7].Controls[0])).Text.ToString();
                    string 
    _Doctor = ((TextBox)(row.Controls[8].Controls[0])).Text.ToString();
                    string 
    _Count = ((TextBox)(row.Controls[9].Controls[0])).Text.ToString();                                                string strSql = "update a set";
                    Run.RunSql(strSql);
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }            GridView1.EditIndex = -1;
                BindData();
            
            }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                BindData(); 
            }
      

  9.   

    你的GridView里的TextBox一出来就是可编辑的吗?不用点编辑按钮再呈现可编辑状态,然后点更新再更新至数据库吗?
      

  10.   

    代码编写的方法我有但是由于我不会写查询条件,就没用了,我就直接用的控件,以前有做成功过一次,
    代码主要我不太会,当时只需要一行,忘记加载到那里的了
    ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text = (string)Session["UserName"];
    这句的作用就是让,我选中行[e.rowindex]的列cells[4]的值是 session传送来的用户名
    然后点更新,更新就可以了
      

  11.   

    你的GridView里的TextBox一出来就是可编辑的吗?不用点编辑按钮再呈现可编辑状态,然后点更新再更新至数据库吗?需要点编辑才可以呈现可编辑状态,然后在点更新在更新到数据库,我想是否可以,点了编辑后就直接把值传给了对应的textbox控件,然后更新的时候,就直接更新了
      

  12.   

    GridView.Controls[0].Controls[0].FindControl("id")//第一个Control为Table,第二个Control为Row
      

  13.   

    你的意思是点了编辑后呈现编辑状态不用你自己输入值,直接在代码里给GridView中的各个TextBox赋值吗?
    你要不把你的代码贴出来看看吧,,实在是不明白你的意思!~~
      

  14.   

    是的直接赋值当前用户名到其中一列
    代码如下页面文件;
    <%@ Page Language="C#" MasterPageFile="~/User2/MasterPage.master" AutoEventWireup="true" CodeFile="CaseLV.aspx.cs" Inherits="User2_CaseLV" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <p>
            <asp:Button ID="Button3" runat="server" Text="未处理" />
            <asp:Button ID="Button4" runat="server" Text="已处理" 
                PostBackUrl="~/User2/CASELV2.aspx" />
        </p>
    <p>
            <asp:TextBox ID="TextBox1" runat="server" Width="78px"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="日期" onclick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="确定" />
        </p>
    <p>
            <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" 
                BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
                Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="87px" 
                onselectionchanged="Calendar1_SelectionChanged" ShowGridLines="True" 
                Visible="False" Width="57px">
                <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                <SelectorStyle BackColor="#FFCC66" />
                <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                <OtherMonthDayStyle ForeColor="#CC9966" />
                <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
                    ForeColor="#FFFFCC" />
            </asp:Calendar>
        </p>
    <p>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
                DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" 
                DataKeyNames="id" onrowupdating="GridView1_RowUpdating" >
                <RowStyle BackColor="#EFF3FB" />
                <Columns>
                    <asp:CommandField ShowEditButton="True" EditText="选择" UpdateText="确定" />
                    <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" 
                        InsertVisible="False" ReadOnly="True" Visible="False">
                    </asp:BoundField>
                    <asp:BoundField DataField="caseid" HeaderText="caseid" 
                        SortExpression="caseid" >
                    <ItemStyle Width="300px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="aggent" HeaderText="aggent" 
                        SortExpression="aggent" Visible="False" />
                    <asp:BoundField DataField="folt2" HeaderText="folt2" 
                        SortExpression="folt2" Visible="False" />
                    <asp:BoundField DataField="folt" HeaderText="folt" 
                        SortExpression="folt" />
                    <asp:BoundField DataField="casetime" HeaderText="升级时间" 
                        SortExpression="casetime" />
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </p>
    <p>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MuserConnectionString2 %>" 
                DeleteCommand="DELETE FROM [caselv] WHERE [id] = @id" 
                InsertCommand="INSERT INTO [caselv] ([caseid], [aggent], [folt2], [folt], [casetime]) VALUES (@caseid, @aggent, @folt2, @folt, @casetime)" 
                SelectCommand="SELECT [id], [caseid], [aggent], [folt2], [folt], [casetime] FROM [caselv] WHERE (([casetime] &gt; @casetime) AND ([folt2] = @folt2))" 
                UpdateCommand="UPDATE [caselv] SET [caseid] = @caseid, [aggent] = getdate(), [folt2] = '已处理', [folt] = @folt, [casetime] = @casetime WHERE [id] = @id">
                <SelectParameters>
                    <asp:ControlParameter ControlID="TextBox1" Name="casetime" PropertyName="Text" 
                        Type="DateTime" />
                    <asp:Parameter DefaultValue="未处理" Name="folt2" Type="String" />
                </SelectParameters>
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="caseid" Type="String" />
                    <asp:Parameter Name="aggent" Type="DateTime" />
                    <asp:Parameter Name="folt2" Type="String" />
                    <asp:Parameter Name="folt" Type="String"/>
                    <asp:Parameter Name="casetime" Type="DateTime" />
                    <asp:Parameter Name="id" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="caseid" Type="String" />
                    <asp:Parameter Name="aggent" Type="DateTime" />
                    <asp:Parameter Name="folt2" Type="String" />
                    <asp:Parameter Name="folt" Type="String" />
                    <asp:Parameter Name="casetime" Type="DateTime" />
                </InsertParameters>
            </asp:SqlDataSource>
            <br />
           </p>
    </asp:Content>
      

  15.   

    使用的数据源的,但是不知道怎么改位置
    其实我的目的是
    我使用gridview显示一个表里面的数据 
    我想实现,在选中一行点编辑的时候,将最后一行绑定的不是数据库的内容,绑定的当前用户名,我用的session保存的用户名 
    然后在点更新,将绑定的内容更新到数据库上 
    使用的是VS2008自带的控件器请帮助下谢谢 
    列如 数据库内标为 列1  列2  列3 
    1    12    3 
    2    22    23 通过此操作后(选择编辑,然后点击更新)后表如下 
    列1  列2    列3 
    1    12    tom 
    2    22      23 
    (tom为当前用户名,通过session在登录时保存的)