-1

Error on RowUpdating >> GridView 'GView' fired the RowUpdating event, which was not handled.

I put the RowEditing and I can enter to update the data, but when I put public void GView_UpdateItem(int id) {} ​​it returns the error saying GridView 'GView' triggered the RowUpdating event, which was not handled.

<asp:GridView runat="server" ID="GView" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing="GView_RowEditing" UpdateMethod="GView_UpdateItem">

    <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>



    <Columns>
        <asp:CommandField ShowEditButton="True"></asp:CommandField>
        <asp:CommandField ShowDeleteButton="True"></asp:CommandField>
    </Columns>

        C# CODE
        
 

protected void Page_Load(object sender, EventArgs e)
        {

        }


        public static List<Contato> contatos = new List<Contato>();



        public void btnCad(object sender, EventArgs e)
        {
            Contato contato = new Contato();

            contato.Id = contatos.Count;
            contato.Nome = this.txtNome.Text;
            contato.Idade = this.txtIdade.Text;
            contato.Telefone = this.txtTelefone.Text;
            contato.Genero = this.txtGenero.Text;
            contato.DataCadastro = this.txtDataCad.Text;


            contatos.Add(contato);
            GView.DataSource = contatos;
            GView.DataBind();
        }

        
        public void GView_UpdateItem(int id)
        {
            VoxTraining.Models.Contato item = contatos.Find(cont => cont.Id == id);
            
            if (item == null)
            {
                
                ModelState.AddModelError("", String.Format("O item com id {0} não foi encontrado", id));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                

            }

        }

        protected void GView_RowEditing(object sender, GridViewEditEventArgs e)
        {

            return ;
        }
  • You have a client (a browser) that connects to a server (The CRUD). Normally the server will have a second interface that connects to the database. I do not see any code that is doing the methods that interfaces with a database in the code posted. – jdweng Jul 06 '22 at 23:15
  • I register without database and without datatables, image in the following link https://imgur.com/a/MtFpf5y btnCad is the button that registers and I use DataBind(); to show the data – lucasmenchon Jul 06 '22 at 23:50
  • https://github.com/lucasmenchon/crud_webforms – lucasmenchon Jul 07 '22 at 00:12
  • What's the issue you are facing? – Chetan Jul 07 '22 at 00:23
  • hard to see what is going on but try putting your data in a Data Table then bind the Grid with that Table. – JobesK Jul 07 '22 at 00:37
  • @JobesK Is it possible to make a crud without database and datatables with webforms .net framework 4.7.2? – lucasmenchon Jul 07 '22 at 00:44
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 07 '22 at 03:19
  • hello, I managed to enter to edit but now it shows another error, **GridView 'GView' triggered the RowUpdating event, which was not handled.** – lucasmenchon Jul 07 '22 at 03:37
  • Looks like you need a GView_RowUpdating event and just return from the event like you code for GView_RowEditing. – jdweng Jul 07 '22 at 05:36
  • Don't bother with the GV edit event - see my post below. Just use clean simple code, and persist your list of names into a list - see below for how this works. – Albert D. Kallal Jul 07 '22 at 20:28

1 Answers1

0

Ok, a few things.

Yes, we can do this without a database. We have have to persist our "list of names".

Dump the delete and edit buttons for the GV - they not worth the trouble - they don't help you.

So, we have the edit area, and the grid.

So, we have this markup:

<div id="myedit" runat="server" style="width:30%;padding:35px;display:none">
    <div cssclass="form-group">
     <label for="exampleInputEmail1">Nome</label>
        <asp:TextBox CssClass="form-control" runat="server" ID="txtNome"></asp:TextBox>
         </div>
         <div cssclass="form-group">
         <label for="exampleInputPassword1">Idade</label>
     <asp:TextBox CssClass="form-control" runat="server" ID="txtIdade"></asp:TextBox>
         </div>
        <div cssclass="form-group">
            <label for="exampleInputPassword1">Telefone</label>
            <asp:TextBox CssClass="form-control" runat="server" ID="txtTelefone"></asp:TextBox>
        </div>
    
    
        <asp:Label runat="server" AssociatedControlID="txtGenero" ><b>Genero</b></asp:Label><br />
        <asp:DropDownList CssClass="form-group" checked="" ID="txtGenero" runat="server" OnSelectedIndexChanged="btnCad">
            <asp:ListItem Text="Masculino" />
            <asp:ListItem Text="Feminino" />
        </asp:DropDownList>
    
        <div cssclass="form-group">
            <label for="exampleInputPassword1">Data de Cadastro</label>
            <asp:TextBox CssClass="form-control" runat="server" TextMode="Date" ID="txtDataCad"></asp:TextBox>
        </div>
        <br />
        <asp:Button runat="server" ID="IDbtnCad" CssClass="btn" Text="Save" OnClick="btnCad" />

        <asp:Button runat="server" ID="btnCancel" CssClass="btn" Text="Cancel" OnClick="btnCancel_Click" 
            style="margin-left:20px" />
        <asp:Button runat="server" ID="btnDelete" CssClass="btn" Text="Delete" OnClick="btnCancel_Click" 
            style="margin-left:30px" />

</div>

    <div id="MyGrid" runat="server">
    <asp:Button ID="cmdNew" runat="server" Text="+Add new" CssClass="btn" OnClick="cmdNew_Click"/>
        <asp:GridView runat="server" ID="GView" CellPadding="4" ForeColor="#333333" cssclass="table" Width="40%"
            ShowHeaderWhenEmpty="true" >        

            <Columns>
                <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Button ID="cmdEdit" runat="server" Text="Edit" CssClass="btn" OnClick="cmdEdit_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"></HeaderStyle>
            
        </asp:GridView>
    </div>

And code to load is this:

    List<Contato> MyNames = new List<Contato>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["MyNames"] = MyNames;
            GView.DataSource = MyNames;
            GView.DataBind();

        }
            
        else
            MyNames = ViewState["MyNames"] as List<Contato>;

    }

Note how we save the list of names.

So, we now have this:

enter image description here

so, add new button is thus this:

   protected void cmdNew_Click(object sender, EventArgs e)
    {

        // add new
        Contato OneName = new Contato();
        MyNames.Add(OneName);
        MyEdit(MyNames.Count - 1);


    }

And my edit is this:

   void MyEdit(int RowIndex)
    {

        Contato OneName = MyNames[RowIndex];

        txtNome.Text = OneName.Nome;
        txtIdade.Text = OneName.Idade;
        txtTelefone.Text = OneName.Telefone;
        txtGenero.Text = OneName.Genero;
        txtDataCad.Text = OneName.DataCadastro;
        ViewState["RowIndex"] = RowIndex;

        // show editor, hide grid

        myedit.Style.Add("Display", "nomral");
        MyGrid.Style.Add("display", "none");


    }

so, we hit add, and we now have this:

enter image description here

We hit save button, this code

   protected void btnCad(object sender, EventArgs e)
    {
        int RowIndex = (int)ViewState["RowIndex"];

        Contato OneName = MyNames[RowIndex];

        OneName.Nome = this.txtNome.Text;
        OneName.Idade = this.txtIdade.Text;
        OneName.Telefone = this.txtTelefone.Text;
        OneName.Genero = this.txtGenero.Text;
        OneName.DataCadastro = txtDataCad.Text;

        ViewState["MyNames"] = MyNames;
        myedit.Style.Add("Display", "none");
        MyGrid.Style.Add("display", "normal");

        GView.DataSource = MyNames;
        GView.DataBind();

    }

And we now have this:

enter image description here

We can use add again, and now this:

enter image description here

the edit row button - plane jane asp.net button click.

Code is this:

    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        Button btnEdit = sender as Button;
        GridViewRow gRow = btnEdit.NamingContainer as GridViewRow;
        MyEdit(gRow.RowIndex);

    }

So, hitting edit button, goes back to the editor.

Say like this:

enter image description here

Cancel button - does nothing (no save, return to grid).

Say this:

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        myedit.Style.Add("Display", "none");
        MyGrid.Style.Add("display", "normal");

    }

And the delete button - that would just remove the current row index item from the list.

eg:

    protected void cmdDel_Click(object sender, EventArgs e)
    {
        myedit.Style.Add("Display", "none");
        MyGrid.Style.Add("display", "normal");

        int RowIndex = (int)ViewState["RowIndex"];
        MyNames.RemoveAt(RowIndex);
        GView.DataSource = MyNames;
        GView.DataBind();

    }
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51