When i try to add a new row to the exsisting grid, my values are getting cleared. Using the Viewstate i maintain the Datatable thorought the page and Bind it to the Grid. Think the values are getting cleared during a postback.
// AddNewRow
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int i;
if (dtCurrentTable.Rows.Count > 0)
{
for (i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox txtguide = (TextBox)grdreports.Rows[rowIndex].FindControl("txtAccNo");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["txtAccNo"] = txtguide.Text;
rowIndex += 1;
}
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
ViewState["rowcount"] = dtCurrentTable.Rows.Count;
grdreports.DataSource = dtCurrentTable;
grdreports.DataBind();
}
}
// aspx Page:
<asp:GridView ID="grdreports" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
S.No</HeaderTemplate>
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Acc.No/Tel.No">
<ItemTemplate>
<asp:TextBox ID="txtAccNo" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "nvrGuide")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AddNew" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="lbtnAddGuide" runat="server" CommandName="Add" Text="Add New"
OnClick="lbtnAddGuide_OnClick"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="lbtnDeleteGuide" runat="server" CommandName="Delete" Text="Delete"
OnClick="lbtnDeleteGuide_OnClick"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Where am i going wrong?
My guess is, the names are different.
Which means,
The name
`Text='<%#DataBinder.Eval(Container.DataItem, "nvrGuide")%>'`
specified in the .aspx page is "nvrguide" but, the name in the .cs page
TextBox txtguide = (TextBox)grdreports.Rows[rowIndex].FindControl("txtAccNo");
is "txtAccNo". So i think the values you have binded as empty grid in .cs page is different from the .aspx page.
Kindly change it as same in both the pages.
Probably it should throw an error for this. Since the code is not totally shown i cant tel.
For page Postback use following code on page load event
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (GridViewRow row in GridView1.Rows)
{
((TextBox)row.FindControl("txtAccNo")).Text = Request.Form[((TextBox)row.FindControl("txtAccNo")).UniqueID];
}
}
}
Related
I have a grid view where data is dyanmically added. I need an onRowCommand where the selected row will be deleted. I need the row to be deleted only from the grid view.
protected void LoadDataTable()
{
string header = lblHeader.Text;
string aa = tv.SelectedNode.Parent.Value;
string child = tv.SelectedNode.Text;
string parent = tv.SelectedNode.Parent.Text;
var dt = new DataTable();
dt.Columns.Clear();
dt.Rows.Clear();
dt.Columns.Add("TargetGLId");
dt.Columns.Add("TargetHead");
dt.Columns.Add("Parent");
dt.Columns.Add("Header");
foreach (GridViewRow row in gvBudgetSetup.Rows)
{
var lblheader = (Label)row.FindControl("lblHeader");
var lblparticular = (Label)row.FindControl("lblParticular");
var lblGlId = (Label)row.FindControl("lblGLId");
var lblparent = (Label)row.FindControl("lblParent");
var dr = dt.NewRow();
dr["TargetHead"] = lblparticular.Text;
dr["TargetGLID"] = lblGlId.Text;
dr["Parent"] = lblparent.Text;
dr["Header"] = lblHeader.Text;
dt.Rows.Add(dr);
}
var dr1 = dt.NewRow();
dr1["TargetHead"] = child;
dr1["TargetGLID"] = tv.SelectedValue;
dr1["Parent"] = parent;
dr1["Header"] = header;
dt.Rows.Add(dr1);
gvBudgetSetup.DataSource = null;
gvBudgetSetup.DataBind();
gvBudgetSetup.DataSource = dt;
gvBudgetSetup.DataBind();
}
I have tried this code to delete the row but it is not working.
protected void gvBudgetSetup_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete1")
{
// gvBudgetSetup.DeleteRow(e.Row.RowIndex);
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int getRowIndex = gvr.RowIndex;
gvBudgetSetup.DeleteRow(getRowIndex);
}
}
This is the Gridview created. The gridview is not connected to any database. The data in the gridview is added from the back-end codes.
<asp:GridView ID="gvBudgetSetup" runat="server" CssClass="table1" AutoGenerateColumns="false" ShowFooter="true" OnSelectedIndexChanged="gvBudgetSetup_SelectedIndexChanged"
ShowHeaderWhenEmpty="true" OnRowDataBound="gvBudgetSetup_RowDataBound" OnRowCommand="gvBudgetSetup_RowCommand" OnRowDeleting="gvBudgetSetup_RowDeleting" Width="100%">
<FooterStyle CssClass="GridFooter" />
<RowStyle CssClass="GridItem" />
<columns>
<asp:TemplateField HeaderText="Sn">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GL Id">
<ItemTemplate>
<asp:Label ID="lblGLId" runat="server" Text='<%# Bind("TargetGLID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header">
<ItemTemplate>
<asp:Label ID="lblHeader" runat="server" Text='<%# Bind("Header") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent">
<ItemTemplate>
<asp:Label ID="lblParent" runat="server" Text='<%# Bind("Parent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Child">
<ItemTemplate>
<asp:Label ID="lblParticular" runat="server" Text='<%# Bind("TargetHead") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Delete1"
ID="lnkDelete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</columns>
<EmptyDataTemplate>
"There are no data to display..."
</EmptyDataTemplate>
</asp:GridView>
Better way to do is have a flag in the data source that one row is flagged(deleted) and bind it again. To delete a datarow from a GridViewEvent, you have to re bind the datasource again.
Introduce a Flag column in your data source
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
//Update the flag in datasource using the CommandArgument value
//Bind the datasource again.
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int getRowIndex = gvr.RowIndex;
gvBudgetSetup.DeleteRow(getRowIndex);
Use this updated code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="301px" AllowPaging="True" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:BoundField DataField="StudentName" HeaderText="StudentName" SortExpression="StudentName" />
<%-- <asp:BoundField DataField="StudentDOB" HeaderText="StudentDOB" SortExpression="StudentDOB" />--%>
<asp:TemplateField HeaderText="DeptName">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100px"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#String.Format("~/Employee/EmployeeEditPage.aspx?StudentID={0}", Eval("StudentID"))%>'> Edit</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
above is my design:
below I wrote my code for to display the database, which have table departmenttable and another table studenttable to show the values into the grid. in department table I have departmentname(dept_name)
which should come in dropdown inside the gridview..but it showing error..pls help..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection con = new SqlConnection(DataBase.GetConnection());
con.Open();
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddList = (DropDownList)e.Row.FindControl("dept_name");
//bind dropdownlist
DataTable dt = con.GetData("select dept_name from dbo.DepartmentTable");
ddList.DataSource = dt;
ddList.DataTextField = "dept_name";
ddList.DataValueField = "DeptName";
ddList.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
//ddList.SelectedItem.Text = dr["YourCOLName"].ToString();
ddList.SelectedValue = dr["DeptName"].ToString();
con.Close();
GridView1.DataBind();
}
}
}
For binding dropdownlist in GriView first you need to find it by its id. In your sample code, you are trying to find something which is not dropdownlist.
Try this,
DropDownList ddList = (e.Row.FindControl("DropDownList1") as DropDownList);
if(ddList != null)
{
//your code
}
I am working in ASP.Net with C# as code behind. The editor is Visual Studio 2012.
I created a GridView with 5 columns: the first one contains checkboxes, the next 3 have text boxes and the last one is a buttonfield, as follows (I have made AutoGenerateColumns as false because, otherwise DataBind adds more columns":
<asp:GridView ID="gvCDS" runat="server" AutoGenerateColumns="False" OnRowDeleting="gvCDS_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelectedCDS" runat="server" Enabled="true"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Code">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleCode" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Name">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleName" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit Units">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSCreditUnits" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Clear Module" />
</Columns>
I need to show the GridView on selecting an item from a dropdown. The GridView always needs to have 8 rows. I have written the following code to show the GridView:
DataTable dt = ViewState["dt"] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("", typeof(Boolean));
dt.Columns.Add("Module Code", typeof(String));
dt.Columns.Add("Module Name", typeof(String));
dt.Columns.Add("Credit Units", typeof(String));
for (int i = 0; i < 8; i++)
{
dt.Rows.Add(dt.NewRow());
}
}
gvCDS.DataSource = dt;
gvCDS.DataBind();
ViewState["dt"] = dt;
gvCDS.Visible = true;
The user makes some inputs in the GridView and then clicks a button. In the button's click event procedure, I need to delete the checked rows from the GridView and add rows so that total number of rows remains as 8. If I simply call .DeleteRow method, the row is not getting deleted. I still see the GridView as earlier in the browser. So I first copy the content of GridView into DataTable as follows:
DataTable dt = ViewState["dt"] as DataTable;
int rowCounter = 0;
foreach (GridViewRow row in gvCDS.Rows)
{
DataRow dr = dt.Rows[rowCounter];
dr[0] = ((CheckBox)row.FindControl("cbSelectedCDS")).Checked;
dr[1] = ((TextBox)row.FindControl("tbxInputCDSModuleCode")).Text;
dr[2] = ((TextBox)row.FindControl("tbxInputCDSModuleName")).Text;
dr[3] = ((TextBox)row.FindControl("tbxInputCDSCreditUnits")).Text;
rowCounter++;
}
After that, instead of deleting rows from the GridView, I delete from the DataTable as follows:
rowCounter = 0;
int rowsDeleted = 0;
foreach (GridViewRow row in gvCDS.Rows)
{
if (((CheckBox)row.FindControl("cbSelectedCDS")).Checked)
{
dt.Rows.RemoveAt(rowCounter);
rowsDeleted++;
}
else
{
rowCounter++;
}
}
rowsDeleted now contains the number of rows that were deleted. I add that many rows in the DataTable so that the total number of rows remains as 8.
for (i = 0; i < rowsDeleted; i++)
{
dt.Rows.Add(dt.NewRow());
}
After that I bind GridView to DataTable as follows:
ViewState["dt"] = dt;
gvCDS.DataSource = dt;
gvCDS.DataBind();
The GridView still has 8 rows. That is correct. But the problem is that the entire GridView has become empty. When testing, I filled some of the rows and checked only one of them. So, the content of the unchecked rows should be there. But the entire GridView is empty. I do not know why.
Not only that, I even tested by keeping all rows unchecked, which means no row will be deleted. Still, the GridView becomes all empty.
How do I solve this problem?
Thank you.
Issue is your Text Boxes inside GridView are not bound to any field. Bind their Texts to the fields like Text='<%# Eval("Field")%>'.
Change your GridView markup to this:
<asp:GridView ID="gvCDS" runat="server" AutoGenerateColumns="False" OnRowDeleting="gvCDS_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSelectedCDS" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Code">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleCode" Text='<%# Eval("Module Code")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Module Name">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSModuleName" Text='<%# Eval("Module Name")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit Units">
<ItemTemplate>
<asp:TextBox ID="tbxInputCDSCreditUnits" Text='<%# Eval("Credit Units")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Clear Module"/>
</Columns>
</asp:GridView>
I have the following GridView which has a couple DropDownLists and TextBoxes. How can I add a new row to it, while persisting the existing GridView. I would like to Add the New row with the LinkButton. I am not using DataSource Controls and the GridView is currently populated via a DataTable. Here is the GridView:
<asp:LinkButton ID="btnAdd" runat="server" Text="Add Room"
onclick="btnAdd_Click"></asp:LinkButton>
<asp:GridView ID="gvRP" runat="server" AutoGenerateColumns="false"
onrowdatabound="gvRP_RowDataBound"
onrowediting="gvRP_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Room" ItemStyle-Width="100%">
<ItemTemplate>
<asp:Label runat="server" Text="Room"></asp:Label>
<asp:DropDownList ID="ddlRoom" runat="server" AutoPostBack="True" DataTextField="Name"
DataValueField="Id" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlRoom_SelectedIndexChanged">
<asp:ListItem Value="-1">Select...</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" AssociatedControlID="ddlRate" Text="Rate" ID="lblRate"></asp:Label><asp:DropDownList
ID="ddlRate" runat="server" AppendDataBoundItems="true" DataTextField="Name"
DataValueField="Id">
<asp:ListItem Value="-1">Select...</asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" Text="Adults"></asp:Label>
<asp:TextBox ID="txtAdults" Text='<%#Bind("Adults") %>' runat="server" Width="25px"></asp:TextBox>
<asp:Label runat="server" Text="Children"></asp:Label>
<asp:TextBox ID="txtChildren" Text='<%#Bind("Children") %>' runat="server" Width="25px"></asp:TextBox>
<asp:Label runat="server" Text="Check In"></asp:Label>
<asp:TextBox ID="txtCheckIn" Text='<%#Bind("CheckIn") %>' runat="server" Width="75px"></asp:TextBox>
<asp:Label runat="server" Text="Check Out"></asp:Label>
<asp:TextBox ID="txtCheckOut" Text='<%#Bind("CheckOut") %>' runat="server" Width="75px"></asp:TextBox>
<h3>Rates</h3>
<asp:GridView ID="gvR" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Rate" />
<asp:BoundField DataField="Effective" HeaderText="Effective" />
<asp:BoundField DataField="Expire" HeaderText="Expire" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
<asp:BoundField DataField="Code" HeaderText="Currency" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Usually I try and do an example, but this one is quite thorough, and I don't "think" the url is going anywhere. Please refer to this link for a comprehensive example.
Here's the important code.
grid
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
</FooterTemplate>
code behind
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid()
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
Put Label control or Textbox as you like inside ItemTemplate if you put it at last it'll be displayed at last, for example
<ItemTemplate>
....
<asp:Label Text="foo" runat="server" />
</ItemTemplate>
or
<ItemTemplate>
<asp:Label Text="foo" runat="server" />
....
</ItemTemplate>
I decided to go with this solution:
DataTable dt = new DataTable();
DataColumn dcRoom = new DataColumn("Room", typeof(DropDownList));
DataColumn dcAdults = new DataColumn("Adults", typeof(string));
DataColumn dcChildren = new DataColumn("Children", typeof(string));
DataColumn dcCheckIn = new DataColumn("CheckIn", typeof(string));
DataColumn dcCheckOut = new DataColumn("CheckOut", typeof(string));
dt.Columns.AddRange(new DataColumn[] { dcRoom, dcAdults, dcChildren, dcCheckIn, dcCheckOut });
dt.Rows.Add(new object[] { new DropDownList(), "", "", "", "" });
gvRP.DataSource = dt;
gvRP.DataBind();
How does it know What to put in the DropDown (Select...) and I haven't specified two DropDowns, yet it still puts the second DropDown.
namespace gridview_row_add
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataGridViewTextBoxColumn columntype = new DataGridViewTextBoxColumn();
columntype.HeaderText = "Type";
columntype.Width = 80;
dataGridView1.Columns.Add(columntype);
DataGridViewTextBoxColumn columnparameters = new DataGridViewTextBoxColumn();
columnparameters.HeaderText = "Parameters";
columnparameters.Width = 320;
dataGridView1.Columns.Add(columnparameters);
DataGridViewTextBoxColumn columndisplay = new DataGridViewTextBoxColumn();
columndisplay.HeaderText = "Display";
columndisplay.Width = 150;
dataGridView1.Columns.Add(columndisplay);
DataGridViewTextBoxColumn enumuration = new DataGridViewTextBoxColumn();
enumuration.HeaderText = "Format";
enumuration.Width = 90;
dataGridView1.Columns.Add(enumuration);
dataGridView1.AllowUserToAddRows = false;//please add this if u don't want to add exta rows or else make it true.
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add();//here on each click the new row will be added.
int rowcount = dataGridView1.Rows.Count;
dataGridView1.Rows[rowcount - 1].Cells[0].Value = "data" + rowcount.ToString();
dataGridView1.Rows[rowcount-1].Cells[1].Value = "field";
dataGridView1.Rows[rowcount-1].Cells[2].Value = "xyzzz";
dataGridView1.Rows[rowcount-1].Cells[3].Value = "hts";
rowcount++;
}
}
}
this code works fine for me. in this code i added four headers in girdview, you can change them as per your requirement..one button click it will add new row first then data got filled in that row..
hope this will work for you..
cs code
protected void DeleteNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["dtCurrentTable"] != null)
{
DataTable DeldtCurrentTable = (DataTable)ViewState["dtCurrentTable"];
DataRow drCurrentRow = null;
if (DeldtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i > DeldtCurrentTable.Rows.Count; i--)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
//TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddldatatype");
drCurrentRow = DeldtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i - 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
//drCurrentRow["Column3"] = box3.Text;
rowIndex--;
}
//add new row to DataTable
DeldtCurrentTable.Rows.Remove(drCurrentRow);
//Store the current data to ViewState
ViewState["dtCurrentTable"] = DeldtCurrentTable;
//Rebind the Grid with the current data
Gridview1.DataSource = DeldtCurrentTable;
Gridview1.DataBind();
}
}
}
protected void ButtonDel_Click(object sender, EventArgs e)
{
DeleteNewRowToGrid();
}
aspx code
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Column Name">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Data Type">
<ItemTemplate>
<asp:DropDownList ID="ddldatatype" runat="server">
<asp:ListItem>varchar</asp:ListItem>
<asp:ListItem>int</asp:ListItem>
<asp:ListItem>numeric</asp:ListItem>
<asp:ListItem>uniqueidentifier</asp:ListItem>
<asp:ListItem>char</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>
<asp:Button ID="ButtonDel" runat="server" Text="Delete Row" OnClick="ButtonDel_Click"/>
<input type="hidden" runat="server" value="0" id="hiddencount" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
I have done with add rwo, now I want to delete
you are trying to deleta a row "drCurrentRow" that's not even in your DataTable, because you created this drCurrentRow = DeldtCurrentTable.NewRow(); yourself and didn't even insert it (no use anyway).
You have to search the rows to delete from your source like this:
var toDel = DeldtCurrentTable.Where(row => /* true if found */).ToArray();
foreach(var t in toDel) DeldtCurrentTable.Rows.Remove(t);
and then you can reiterate and set your rownumbers again.
Finaly set the the ViewState and the DataSource + DataBind it
BTW: your naming is really frustrating - what the heck does "DeleteNewRowToGrid" mean? Do you want to delete some new row? What is the a new row in this case?
So: Which row should be deleted here?