2

So I have google'd and searched stackoverflow, and now my brain is overloaded. I am a novice at asp.net, but getting the hang of it.

My current requirement is to have a gridview where upon load, 1 column for all rows is immediately placed into edit mode. I used this question and code to get me going:

Allowing one column to be edited but not another

<asp:gridview id="CustomersGridView"      
              datasourceid="CustomersSqlDataSource"      
              autogeneratecolumns="false"     
              autogenerateeditbutton="true"     
              allowpaging="true"      
              datakeynames="CustomerID"       
runat="server">      
    <columns>       
        <asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
        <asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/> 
        <asp:boundfield datafield="Address" headertext="Address"/>       
        <asp:boundfield datafield="City" headertext="City"/>       
        <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>     
    </columns> 
</asp:gridview>

I have searched and found a few good solutions, however, I do not fully understand them and have been unsuccesful at implementing them. They are:

Edit/Update a single GridView field

Put multiple rows of a gridview into edit mode

So my question is, how would you go about placing a column, (e.g, ZIP Code) into edit mode for all rows at the same time?

All help is appreciated!

Thanks!

Stephen

Community
  • 1
  • 1
stephen99999
  • 35
  • 1
  • 6

1 Answers1

1

You won't be able to use the built-in edit functionality, but you can achieve this by loading the column in edit mode using a TemplateField:

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" ...>    
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SomeColumn") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="SomeOtherColumn" HeaderText="Foo" />
        ...
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" CommandName="Update" CommandArgument='<%# Container.ItemIndex %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-behind:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow row = GridView1.Rows[(int)e.CommandArgument];
    if (row != null)
    {
        TextBox txt = row.FindControl("TextBox1") as TextBox;
        if (txt != null)
        {
            //get the value from the textbox
            string value = txt.Text;
        }
    }
}

EDIT: Putting a button outside of the GridView, you would update like this:

<asp:GridView>
    ...
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />

Code-behind:

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        TextBox txt = row.FindControl("TextBox1") as TextBox;
        if (txt != null)
        {
            //get the value from the textbox
            string value = txt.Text;
        }
    }
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Will this still allow me to keep it simple and place the rest of the columns in boundfields? Also, will this allow me to update all rows, or one row at a time? – stephen99999 Nov 04 '11 at 19:09
  • You can use bound fields for the rest of the columns, and yes that column will be editable in all rows. – James Johnson Nov 04 '11 at 19:11
  • ok last question before i check mark ya. Could you elaborate on the need and use of the Button1? – stephen99999 Nov 04 '11 at 19:45
  • `Button1` triggers the `GridView.RowCommand` event where you update the database. It passes the row index in the command argument. I think you could use the `GridViewCommandEventArgs.CommandSource` to get the row too though. – James Johnson Nov 04 '11 at 19:48
  • How could I do it without having a button on each row? I need to be able to update all rows using a button outside of the gridview. – stephen99999 Nov 07 '11 at 19:09
  • Did you unaccept my answer? You could have left it and just asked. You can put a button outside of the GridView, and loop through each row in the GridView. – James Johnson Nov 07 '11 at 19:12
  • Sorry, wasn't sure if it notifies you of the comments or not. Do you mind giving a small example? Would there still be a need for the RowCommand event? THanks for all this help, its greatly appreciated. I have been thinking about using a repeater instead, but not sure. – stephen99999 Nov 07 '11 at 19:24
  • 1
    No, you shouldn't need the `RowCommand` event if you're updating from outside of the GridView. The `RowCommand` event is only fired to get a reference to the row that was clicked. Since you're updating outside of the GridView, you can just loop through all the rows, like in my example above. – James Johnson Nov 07 '11 at 19:31
  • Does gridview's AutoPostBack have to be = true everytime you do a databind()? – stephen99999 Nov 10 '11 at 15:25
  • 1
    No, that shouldn't be necessary if you're using a button outside the GridView to perform the update. – James Johnson Nov 10 '11 at 15:26
  • No problem. That's what we're here for ;) – James Johnson Nov 10 '11 at 15:56