Visual Studio .NET - Change Gridview to edit mode programmatically

Asked By Guillermo Daniel Polonsky on 07-Aug-06 12:17 PM
Hi! I have a gridview, and the datasource it's datatable (i'm not using objectdatasource because i'm not saving nothing to the database, it's just for loading something and then save it to the database).
I have two template columns with buttons, when i click the edit button i want the gridview to change to edit mode, that doesn't happen automatically, how can i change it programmatically?
Thanks in advance. Guillermo.

CommandName - Asked By F Cali on 07-Aug-06 12:26 PM

Make sure that the CommandName of your edit button is set to "Edit".  The gridview will automatically change to Edit mode when this is set.

How to Edit GridView With-Out Datasource Control - Asked By Erik Little on 07-Aug-06 12:58 PM

http://www.ConstructionSuperCenter.Com/SoftWare/Example/Controls/GridView/Edit/Edit-GridView-without-DatasourceControl.zip

erik

CommandField - Asked By Ramya T on 07-Aug-06 01:00 PM

You can use CommandField and set ShowEditButton to true to automatically have edit feature to your gridview. The same way set to true for ShowCancelButton, ShowDeleteButton, ShowInsertButton and ShowSelectButton.

[CODE]

<asp:CommandField ButtonType="Button" ShowCancelButton="true"
ShowDeleteButton="true" ShowEditButton="true" CancelText="Cancel"                EditText="Edit" DeleteText="Delete" />

[/CODE]
CommandArgument empty - Asked By Guillermo Daniel Polonsky on 07-Aug-06 01:22 PM
Could be possible that what you sent doesn't have all .cs files? Beacause you're not calling to edit mode programmatically...

My problem now is this...
I'm getting an empty commandargument...so i can't change to edit mode...

This is my GridView definition

                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                    AutoGenerateColumns="False" EmptyDataText="No se encontraron registros" Height="5px"
                    meta:resourcekey="GridView1Resource1" Width="563px" OnRowCommand="GridView1_RowCommand" CaptionAlign="Left" ShowFooter="True" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="numero_serie" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Producto" meta:resourcekey="TemplateFieldResource1">
                        <ItemTemplate>
                        <asp:Label ID="lbl_product" Text='<%# Eval("producto") %>' runat="server" meta:resourcekey="lbl_productResource1"></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                             <asp:DropDownList ID="ddl_GridView1_producto" runat="server" DataTextField="desc_tipo_producto"
                    DataValueField="id_tipo_producto" meta:resourcekey="ddl_productoResource1" DataSourceID="ObjectDataSource1">
                            </asp:DropDownList> 
                            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get_Tipos_Producto"
                                TypeName="CC_COM.TABLA"></asp:ObjectDataSource>
                        </FooterTemplate>
                            <EditItemTemplate>
                                 
                                <asp:DropDownList ID="ddl_GridView1_producto" runat="server" DataTextField="desc_tipo_producto"
                    DataValueField="id_tipo_producto" meta:resourcekey="ddl_productoResource1" DataSourceID="ObjectDataSource1" SelectedValue='<%# Bind("id_producto") %>'>
                                </asp:DropDownList><asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get_Tipos_Producto"
                                    TypeName="CC_COM.TABLA"></asp:ObjectDataSource>
                            </EditItemTemplate>
                            <HeaderStyle CssClass="DataGridHeader" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Volúmen" meta:resourcekey="TemplateFieldResource2">
                        <ItemTemplate>
                        <asp:Label ID="lbl_volumen" Text='<%# Eval("volumen") %>' runat="server" meta:resourcekey="lbl_volumenResource1"></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txt_footer_volumen" runat="server" CssClass="TextBox" Width="91px" meta:resourcekey="txt_footer_volumenResource1"></asp:TextBox> 
                        </FooterTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_volumen" Text='<%# Eval("volumen") %>' CssClass="TextBox" runat="server"></asp:TextBox> 
                            </EditItemTemplate>
                            <HeaderStyle CssClass="DataGridHeader" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Número de serie" meta:resourcekey="TemplateFieldResource3">
                        <ItemTemplate>
                        <asp:Label ID="lbl_numero_serie" Text='<%# Eval("numero_serie") %>' runat="server" meta:resourcekey="lbl_numero_serieResource1"></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txt_footer_numero_serie" MaxLength="12" runat="server" CssClass="TextBox" Width="90px" ValidationGroup="gridView_textboxes" meta:resourcekey="txt_footer_numero_serieResource1"></asp:TextBox>  
                        </FooterTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_numero_serie" Text='<%# Eval("numero_serie") %>' MaxLength="12" CssClass="TextBox" runat="server"></asp:TextBox> 
                            </EditItemTemplate>
                            <HeaderStyle CssClass="DataGridHeader" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Cód. Seg." meta:resourcekey="TemplateFieldResource4">
                        <ItemTemplate>
                        <asp:Label ID="lbl_clave" Text='<%# Eval("clave") %>' runat="server" meta:resourcekey="lbl_claveResource1"></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txt_footer_clave" runat="server" MaxLength="12" CssClass="TextBox" Width="51px" ValidationGroup="gridView_textboxes" meta:resourcekey="txt_footer_claveResource1"></asp:TextBox>  
                        </FooterTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="txt_clave" Text='<%# Eval("clave") %>' MaxLength="4" CssClass="TextBox" runat="server"></asp:TextBox> 
                            </EditItemTemplate>
                            <HeaderStyle CssClass="DataGridHeader" />
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False" meta:resourcekey="TemplateFieldResource5">
                            <EditItemTemplate>
                                <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="Update" ImageUrl="~/images/Botones/ok.gif" AlternateText="Update" meta:resourcekey="ImageButton1Resource1"></asp:ImageButton> 
                                <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" CommandName="Cancel" ImageUrl="~/images/Botones/goma.gif" AlternateText="Cancel" meta:resourcekey="ImageButton2Resource1"></asp:ImageButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="Edit" ImageUrl="~/images/Botones/modif.gif" AlternateText="Edit" meta:resourcekey="ImageButton1Resource2" /> 
                                <asp:ImageButton ID="ImageButton4" runat="server" CausesValidation="False" CommandName="Delete" ImageUrl="~/images/Botones/eliminar.gif" AlternateText="Delete" meta:resourcekey="ImageButton4Resource1" />
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Button ID="btn_nuevo_certificado" CommandName="ADD" runat="server" Text="Nuevo" CssClass="Botones" ValidationGroup="gridView_textboxes" meta:resourcekey="btn_nuevo_certificadoResource1" /> 
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <RowStyle CssClass="DataGridItem" />
                    <PagerStyle CssClass="DataGridPager" ForeColor="White" />
                    <HeaderStyle CssClass="DataGridHeader" ForeColor="White" />
                    <AlternatingRowStyle CssClass="DataGridAlternatingItem" />
                </asp:GridView>


and my .cs is this

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DataRow dr;
        switch (e.CommandName)
        { 
            case "ADD":
            case "Edit":
                GridView1.FooterRow.Visible = false;
                string id_producto = ((DropDownList)GridView1.FooterRow.FindControl("ddl_GridView1_producto")).SelectedValue;
                GridView1.Rows[System.Int16.Parse(e.CommandArgument.ToString())].RowState = DataControlRowState.Edit;
                break;
            case "Update":
                GridView1.FooterRow.Visible = true;
                ViewState["numero_serieID"] = GridView1.DataKeys[GridView1.EditIndex].Value;
                break;
            case "Cancel":
                GridView1.FooterRow.Visible = true;
                break;
        }
    }
you can not put the gridview in edit - Asked By Erik Little on 07-Aug-06 02:22 PM
mode from the command block.
you need to put the gridview in edit mode in the edit block of the gridview. When you assign the word EDIT to a button inside your gridview it will automatically fire the RowEditing event..

erik
Doesn't work - Asked By Guillermo Daniel Polonsky on 08-Aug-06 03:17 PM
I have put breakpoint int RowEditing and it stops there, but when i see the page it's not in edit mode, i have send the code of my .aspx and .cs files, what could i do to put it int edit mode?
If you are still trying to put the - Asked By Erik Little on 08-Aug-06 03:26 PM
gridview in edit mode from the code behind. I  posted a link yesterday with a fully working example of exactly what you were asking for.

I do understand thow that it is a little tricky to get use to. before the .net 2005 there was only one way to put the datagrid into edit mode and that was programitically. Now with the new datasource controls it makes it a little confusing becuse the gridveiw is really geared for the datasource control. I did not like it at first but after playing around with it the datasource controls are quite nice to have.

anyway here is the link again..

http://www.ConstructionSuperCenter.Com/SoftWare/Example/Controls/GridView/Edit/Edit-GridView-without-DatasourceControl.zip
Have you got some example with datatable - Asked By Guillermo Daniel Polonsky on 08-Aug-06 06:21 PM
as datasource? i mean, don't want to link it to database, just in memory, i don't know if i need to use objectdatasource, dataset or what....
Thanks again. Guillermo.
Ha ha man i feel your pain I wish there - Asked By Erik Little on 08-Aug-06 08:24 PM
was something more that i could do.. 

In that example that i made for you it is using a datatable as it's datasource.. I am only using a database to load the datatable; however, i did load it all just like the .net2003 way with out the datasource control.

What it the source for your data for this datatable anyway?

erik
I explain to you - Asked By Guillermo Daniel Polonsky on 09-Aug-06 08:48 AM
It's a webform that contains payment data, but the user can pay with somthing like discounts, so he need to load the discount serial number, volume, etc, (they can be more than one), and that information i'm storing it in the datatable (datasource of the gridview) so if he want to change it, he can click on the last column (button edit) and change it.
Thanks!. Guillermo.
The only difference i see between - Asked By Guillermo Daniel Polonsky on 09-Aug-06 10:04 AM
the code you have provided and mine it's that i use imagebutton, could that be why when i press the imagebutton edit i don't see the textboxes of edit mode?