Jump to content

GridView Editável em C#


Darley Mendonca

Recommended Posts

Prezados,

Boa tarde!

Estou desenvolvendo uma ferramenta, em C#, e estou com a necessidade de editar dados listados em um GidView.

Ao acionar o método para de edição do Grid, eu consigo editar normalmente os dados da linha selecionada, e quando vou pegar a informação editada via Grid para salvar, a informação retornada do Grid não é a informação editada, e sim a informação carregada anterior a edição.

Alguém consegue me ajudar?

O que estou fazendo de errado?

Segue código ASP do grid:

<asp:GridView ID="GwTeste" 
         runat="server" 
         CellPadding="4" 
         ForeColor="#00498A" 
         GridLines="None" 
         PageSize="15"
         AllowPaging="True" 
         AutoGenerateColumns="False" 
         DataKeyNames="chave"
         OnRowCancelingEdit="GwTeste_RowCancelingEdit" 
         OnRowEditing="GwTeste_RowEditing"
         OnRowUpdating="GwTeste_RowUpdating"
         OnPageIndexChanging="GwTeste_PageIndexChanging" 
         OnSelectedIndexChanged="GwTeste_SelectedIndexChanged" 
	 OnRowDataBound="GwTeste_RowDataBound">     

     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
     <Columns>
         <asp:BoundField DataField="chave" HeaderText="Código" />
         <asp:TemplateField HeaderText="Inform">
             <ItemTemplate>
                 <%# Eval("descricao") %>
             </ItemTemplate>
             <EditItemTemplate>
                 <asp:TextBox ID="txbDescricao"
                     runat="server"
                    Text='<%# Eval("descricao") %>' >
                 </asp:TextBox>
             </EditItemTemplate>
         </asp:TemplateField>
         <asp:CommandField
             CancelImageUrl="~/Icons/xRedondo256x256.png"
             DeleteImageUrl="~/Icons/picaPapel256x256.png"
             EditImageUrl="Icons/lapis-512x512.png"
             EditText=""
             HeaderText="Editar 1"
             ButtonType="Image"
             ShowEditButton="True"
             ShowDeleteButton="True" 
             CausesValidation="False" 
	     UpdateImageUrl="~/Icons/salvar128x128.png">
         <ControlStyle CssClass="icoGrid" />
         <HeaderStyle CssClass="gridHeaderWidth" />
         </asp:CommandField>
     </Columns>
     <EditRowStyle BackColor="#CCCCCC" />
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
     <SortedAscendingCellStyle BackColor="#E9E7E2" />
     <SortedAscendingHeaderStyle BackColor="#506C8C" />
     <SortedDescendingCellStyle BackColor="#FFFDF8" />
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

 

Código C#

protected void GwTeste_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GwTeste.EditIndex = -1;
    carregaGrid();
}

protected void GwTeste_RowEditing(object sender, GridViewEditEventArgs e)
{
    GwTeste.EditIndex = e.NewEditIndex;
    carregaGrid();
}

protected void GwTeste_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int id = Convert.ToInt32(GwTeste.DataKeys[e.RowIndex].Value.ToString());
	
  	TextBox txbDescricao = (TextBox)GwTeste.Rows[e.RowIndex].FindControl("txbDescricao");
    
	GwTeste.EditIndex = -1;
	carregaGrid();
}

protected void GwTeste_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GwTeste.PageIndex = e.NewPageIndex;
    carregaGrid();
}

protected void GwTeste_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowState == 
        (DataControlRowState.Edit| DataControlRowState.Alternate)
            | e.Row.RowState == DataControlRowState.Edit)
    {
        return;
    }
}

 

Desde já agradeço!!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.