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
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();
}
}
----------------------------------------------------------------
MY ASPX CODE :
<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>
</asp:Content>