Click here to Skip to main content
14,830,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to inquire about the delete the data in gridview with automatic number parameter ..
when I edit a field with auto parameter number, how do I delete a field that does not automatically sort ..
ex: 1. ROBERT 2. EMILIA 3. JOSE
when I remove the column to 2 EMILIA, then display in gridview 1. ROBERT 3. JOSE.
coding that I got when I remove emilia then display in gridview 1.ROBERT 2.JOSE .. (sequence automatically)
  Your answers will be very helpful, thanks..
THIS IS MY ASPX CODE :
C#
 <asp:GridView ID="GridCustomColumn" runat="server" AutoGenerateColumns="false"
       ShowFooter="True" CellPadding="4" ForeColor="#333333"
       Width="874px" Height="103px" AllowSorting="True" OnRowCancelingEdit="GridCustomColumn_RowCancelingEdit"
       OnRowDeleting="GridCustomColumn_RowDeleting" OnRowDataBound="GridCustomColumn_RowDataBound"
       OnRowEditing="GridCustomColumn_RowEditing" OnRowUpdating="GridCustomColumn_RowUpdating"
       OnSorting="GridCustomColumn_Sorting" OnPageIndexChanging="GridCustomColumn_PageIndexChanging"
           PageSize="20" AllowPaging="True" GridLines="None">
           <Columns>
            <asp:TemplateField HeaderText="COLUMN">
                                  <ItemTemplate>
                                         <asp:Label ID="lblcolumn" Width = "20px" runat="server" ForeColor="Black"
                                            Text='<%# Eval("column") %>' ></asp:Label>
                                     </ItemTemplate>
                                 </asp:TemplateField>
                                 <asp:TemplateField HeaderText="TEXT">
                                     <ItemTemplate>
                                        <asp:Label ID="lbltextcol" Width = "80px" runat="server" Text='<%# Eval("text")%>'></asp:Label>
                                     </ItemTemplate>
                                       <EditItemTemplate>
                               <asp:TextBox ID="txttextcol" Width = "80px" runat="server" Text='<%# Eval("text")%>'></asp:TextBox>
                                       </EditItemTemplate>
                                       <FooterTemplate>
                               <asp:TextBox ID="txttextcol" Width = "80px" runat="server" Text='<%# Eval("text")%>'></asp:TextBox>
                                       </FooterTemplate>
                                     <ControlStyle Width="80px" />
                                     <HeaderStyle ForeColor="White" />
                                 </asp:TemplateField>
                                 <asp:TemplateField HeaderText="SIZE">
                                     <ItemTemplate>
                                         <asp:Label ID="lblsize" Width = "80px" runat="server" Text='<%# Eval("size")%>' ></asp:Label>
                                     </ItemTemplate>
                                        <EditItemTemplate>
                               <asp:TextBox ID="txtsize" Width = "80px" runat="server" Text='<%# Eval("size")%>' ></asp:TextBox>
                                       </EditItemTemplate>
                                       <FooterTemplate>
                               <asp:TextBox ID="txtsize" Width = "80px" runat="server"></asp:TextBox>
                                      </FooterTemplate>
                                     <ControlStyle Width="80px" />
                                     <HeaderStyle ForeColor="White" />
                                 </asp:TemplateField>

               <asp:TemplateField>
</asp:GridView>


THIS IS MY ASPX.CS CODE :
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           Bindemptydt();
        }
    }
 
    private void Bindemptydt()
    {
        //Declare a datatable for the gridview
        DataTable dt = new DataTable();
        //Add Columns to the datatable
        dt.Columns.Add("COLUMN");
        dt.Columns.Add("TEXT");
        dt.Columns.Add("SIZE");

        //Define a datarow for the datatable dt
        DataRow dr = dt.NewRow();

        //Now add the datarow to the datatable
        dt.Rows.Add(dr);
        //Now bind the datatable to gridview
        GridCustomColumn.DataSource = dt;
        GridCustomColumn.DataBind();

        //Now hide the extra row of the grid view
        GridCustomColumn.Rows[0].Visible = false;
        //Delete row 0 from the datatable
        dt.Rows[0].Delete();
        dt.AcceptChanges();
        //View the datatable to the viewstate
        ViewState["columnreport"] = dt;
    }

protected void GridCustomColumn_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
      ((DataTable)ViewState["columnreport"]).Rows[e.RowIndex].Delete();
      ((DataTable)ViewState["columnreport"]).AcceptChanges();

      if (((DataTable)ViewState["columnreport"]).Rows.Count > 0)
      {

          GridCustomColumn.DataSource = (DataTable)ViewState["columnreport"];

          GridCustomColumn.DataBind();
      }
      else
      {
          Bindemptydt();
      }
  }

    private void addcolumn()
    {
        int i;
        //Add the items to the gridview
        DataTable dt = new DataTable();
        //Assign the viewstate to the datatable
        dt = (DataTable)ViewState["columnreport"];

        for (i = 1; i <= Convert.ToInt32(txtcolumn_count.Text); i++)
        {
     
            DataRow dr = dt.NewRow();
            //dr["report_id"] = txtreport_id.Text;
            dr["COLUMN"] = "";
            dr["TEXT"] = " ";
            dr["SIZE"] = " ";

            //Add the datarow to the datatable
            dt.Rows.Add(dr);
            //Now bind the datatable to the gridview
            GridCustomColumn.DataSource = dt;
            GridCustomColumn.DataBind();


            //Add the details to viewstate also
            ViewState["columnreport"] = dt;
        }
    }
    protected void GridCustomColumn_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblSerial = (Label)e.Row.FindControl("lblcolumn");
            lblSerial.Text = ((GridCustomColumn.PageIndex * GridCustomColumn.PageSize) + e.Row.RowIndex + 1).ToString();
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        addcolumn();
    }



[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
   
Which column is showing you that number?
Member 11165607 5-Dec-14 3:11am
   
COLUMN, lblcolumn in gridview control ...
   
See my answer.

1 solution

For lblcolumn, you have bound "column" data in Markup like...
ASP.NET
<asp:label id="lblcolumn" width="20px" runat="server" forecolor="Black" text="<%# Eval("column") %>" xmlns:asp="#unknown"></asp:label>

Again, inside the GridCustomColumn_RowDataBound, you are changing its value.

So, when GridCustomColumn_RowDeleting is fired, it rebinds the Grid if the below is satisfied.
C#
if (((DataTable)ViewState["columnreport"]).Rows.Count > 0)

So, it goes to GridCustomColumn_RowDataBound Event and changes the value of lblcomumn as 1, 2, 3 etc...

But Markup again changes the value to the ViewState column data. That's why you see different data.

So, I think, if you delete the column binding from the Mark up, it will be fine. Try once.
ASP.NET
<asp:label id="lblcolumn" width="20px" runat="server" forecolor="Black" xmlns:asp="#unknown">Text='<%# Eval("column") %>'></asp:label>
Comments
Member 11165607 5-Dec-14 3:48am
   
well thank you to answer, very helpful!
May I ask once again? how to make a row updating appear before the row editing when load the gridview?
   
First of all, please accept the answer. :)

Next, about your question. Row Updating and Row Editing are Events, not methods. You cannot control their behaviour. They will occur if you do something with the Grid from UI. Like, if you hit the "Edit" Button, it will fire the Row Editing for you. Then if you click on "Update", it will fire the Row Updating Event. These events are independent.

Feel free to ask any doubts. :)

Thanks,
Tadit
Member 11165607 5-Dec-14 4:00am
   
what if I want to display the update event without clicking the edit button? because I wanted to directly update the column when the gridview is loaded for the first time.
   
What???? Update when loaded? Does not make sense to me. When loaded, you are showing some records. When you modify the records from Grid, then only you need to update.
Member 11165607 5-Dec-14 5:37am
   
sorry to make you confused, I mean when loaded .. in gridview, like lbltextcol control becomes (txttextcol) textbox control continuously update and cancel buttons appear when I click the update button, txtcolumn Become lblcolumn, so I can directly update some records .. without click edit first, so right click update button
   
As far as I understand, you want to show the TextBoxes instead of Labels while loaded, so that User can edit directly.

If the above is true, then do one thing. Remove the EditItemTemplates and move those TextBoxes inside the ItemTemplates. That will work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900