Geeks With Blogs

News

Microsoft MVP


Moderator at CodeASP.NET


Quiz Master




free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

In my previous post, I have posted the updated the codes about Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes because there is a bug on that. So obviously the codes in my other example about Adding a Delete functionality in Dynamic TextBoxes in GridView is affected. To fix the issue then you can refer to this updated code below:

 

    protected void LinkButton1_Click(object sender, EventArgs e)

    {

        LinkButton lb = (LinkButton)sender;

        GridViewRow gvRow = (GridViewRow) lb.NamingContainer;

        int rowID = gvRow.RowIndex;

        if (ViewState["CurrentTable"] != null)

        {

          DataTable  dt = (DataTable)ViewState["CurrentTable"];

          if (dt.Rows.Count > 1)

          {

              if (gvRow.RowIndex < dt.Rows.Count -1)

              {

                  //Remove the Selected Row data

                  dt.Rows.Remove(dt.Rows[rowID]);

                  ResetRowID(dt);

              }

          }

            //Store the current data in ViewState for future reference

            ViewState["CurrentTable"] = dt;

            //Re bind the GridView for the updated data

            Gridview1.DataSource = dt;

            Gridview1.DataBind();

        }

 

        //Set Previous Data on Postbacks

        SetPreviousData();

    }

 

    private void ResetRowID(DataTable dt)

    {

        int rowNumber = 1;

        if (dt.Rows.Count > 0)

        {

            foreach (DataRow row in dt.Rows)

            {

                row[0] = rowNumber;

                rowNumber++;

            }

        }

    }

 

If you notice, I have added the method ResetRowID() so that the row number will be sequenced when deleting a row in the Grid.

 

That’s it! Happy coding!

Technorati Tags: ,,
Posted on Wednesday, August 12, 2009 5:21 PM ASP.NET , C# , GridView | Back to top


Comments on this post: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
hi
vinz
if i place my delete button outside of the gridview then what changes i will do and
if i have more two controls outside of the gridview moveup and movedown for moving rows up and down
can u suggest these things ?
Left by warangal on Nov 06, 2009 3:12 AM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
I like how your example works, but I'd like to make the rowID unique so it doesnt' reset... how would I do this? I tried setting the a global rowId var in viewstate and using that, but I'm not setting it in the right place... when I add a row it has the same data as the first row. I need the rowid unique because other controls key off it.

Thank you!!
Left by Tim on Jan 22, 2010 5:01 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Thank you for the quick reply!

Yes I used that article first, but I've implemented the delete functionality, and that's not going to work since we're removnig rows now. I then tried using a uniqueRowID int outside of the method (viewstate int property) but I'm not setting it properly. Can you send me a snippet of where I'd add this? I'm assuming I need to set it in AddNewGridRow when setting the RowID, as well as when adding the current row outside of the for loop.

I have to admin this is because I way you're adding the new row is odd to me... would it work if the persisting logic was moved in a common method that both AddNewRow and DeleteRow could use?

Thank you!!

Left by Tim on Jan 22, 2010 5:37 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
This has helped me a lot so far, thank you for the great post.

For clarification, I need to keep the rowID static after it's been set because I have a nested GridView ... when binding I use the rowID to link the main row with it's nested GridView. So using ResetRowID won't work for me.

I was trying to take the persisting portion out of AddNewRowToGrid() and moving it into it's own method that two methods could call (i.e. AddRow and DeleteRow) but I'm having a hard time with that because the logic isn't simple to me.

I belive it should work if I create a global viewstate variable like _uniqueRowID, but I'm not sure how to set it around:

drCurrentRow["RowNumber"] = i + 1;

and:

dtCurrentTable.Rows.Add(drCurrentRow);


Thanks again,
Tim
Left by Tim on Jan 22, 2010 6:25 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
hi vinz,

Great work. I used your code and it works like a charm however, I want to Edit the size of the textbox inside the inserted row. Is that possible?
Left by LadyBug on Mar 25, 2010 8:22 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
hi,

I tried putting this code into ajax tab container (in a tab panel), but it seems does not work properly with ajax.Any suggestions to get over this problem????

Thanks
Left by abhay on Apr 28, 2010 9:59 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Hi
When I click the remove button, the dropdown is reset automatically, How can i slove this issue
Left by Haji on Aug 11, 2010 2:03 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
The code works great! Thanks a lot for the code! I wanted to know the logic behind setting the rownumbers. Is there any place where you have explained that? Please let me know.
Left by tp on Jan 07, 2011 8:43 AM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
hi vinc thanks for the code...i tried the previous one..but when i add the REMOVE column..i've got compilation erroreven when i debug only the aspx source code..

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30456: 'Gridview1_RowCreated' is not a member of 'ASP.webform1_aspx'.

Source Error:

Line 9: <body>
Line 10: <form id="form1" runat="server">
Line 11: <asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
Line 12:
Line 13: AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated">


can you please help ...
Left by zorrs on Jun 02, 2011 12:57 AM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
@zorrs,

Make sure that your ASPX is pointing to your code behind file. So if you have this:

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Webform1.aspx.cs" Inherits="YourNamespace.WebForm1" %>

Make sure that you have something like this:

namespace YourNamespace
{
public partial class WebForm1: System.Web.UI.Page
{

}
}


If this won't work then try to re create your webform from scratch.
Left by Vinz on Jun 02, 2011 2:13 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
After deleting a row if i add new row,remaining rows value are getting changed.
Left by s.varun on Jun 21, 2011 12:56 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Hello,

I get ORA-00911 : invalid character when trying to do bulk insert to oracle DB.

Can you please help me why? This is what I am doing :

OracleConnection con = new OracleConnection(ConnectionString);

OracleCommand cmd = new OracleCommand(sb.ToString(),con);

try
{

con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
string msg = "Insert Error:";
msg += err.Message;
throw new Exception(msg);

}
finally
{
con.Close();
}




Left by asp.net_guy on Jul 16, 2011 6:20 AM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Hi Vinz,

Could you please guide me in the right direction?

My problem is that i can't get the saved data back into the textboxes as i am saving more than one row into the database, for example:
(text text text
text text text)

I am using nested datalist to display my data as shown above.

In my Parent datalist's Item Command I am looping through my child datalist's items and then recreating the same amount of rows and textboxes and then setting the strings to the saved data text.

This is my code inside my datalist Item command:

Dim ChildDatalist As DataList

Dim dr As DataRow = Nothing
Dim dt As New DataTable()

ChildDatalist = CType(e.Item.FindControl("dlExamSpesific"), DataList)

dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("Column1", GetType(String)))
dt.Columns.Add(New DataColumn("Column2", GetType(String)))
dt.Columns.Add(New DataColumn("Column3", GetType(String)))

For Each ListRow As DataListItem In ChildDatalist.Items

dr = dt.NewRow()

dr("RowNumber") = 1
dr("Column1") = "textboxText"
dr("Column2") = "textboxText"
dr("Column3") = "textboxText"

dt.Rows.Add(dr.ToString)

ViewState("CurrentTable") = dt

Next

This code creates the correct amount of rows with textboxes, but its not inserting the string text into the textboxes...

BASICALY, HOW CAN I POPULATE THE SAVED TEXT BACK INTO THE MULTIPLE ROWS OF TEXTBOXES?

PLEASE can you help me, I have been struggling with this for so long now, and no one in my team can help me.

Thank you
Left by Lilly on Jul 28, 2011 10:13 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
thanks for the code...

if i enter four columns all with values
and i want to remove second column,
the second column and forth column are deleted
i.e while removing last column values are also deleted.
please guide me ..how to overcome this problem ,its too urgent
thanks in advance
Left by Aravindh on Aug 10, 2011 5:12 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
@Aravindh,

The least thing that you can do is debug your codes, set a break point to the event where you want to look at and then step into it so that you'll figure out what;s going on there.
Left by Vinz on Aug 10, 2011 5:27 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
@Vinz,
Thank you for the quick reply.
By setting breakpoint, I came to know that,the last row of the Gridview is not available in datatable... i.e.The Last Row values of textboxes are not preserved.Now What Should i do..?
Left by Aravindh on Aug 11, 2011 1:53 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
@Vinz,
Thank You for Your Guidance.
to solve my problem,
1) i modified the condition like,
if i<dt.rows.count (instead of giving 'if i<dt.rows.count-1 ) which is in the function SetPreviousData()
2)and also i added few lines in LinkButton1_Click() like,
.
.
if (ViewState["CurrentTable"] != null)

{

DataTable dt = (DataTable)ViewState["CurrentTable"];
'after the above said line , i have added the following one

ViewState["CurrentTable"] = dt;


for (int i = 0; i < dt.Rows.Count - 1; i++)
{
'extract the textbox and dropdown values
}

ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data

Gridview1.DataSource = dt;

Gridview1.DataBind();
}



//Set Previous Data on Postbacks

SetPreviousData();

}

if i did like the above said... my problem is solved...
is this right way?
thanks in advance...











if (dt.Rows.Count > 1)
{

if (gvRow.RowIndex < dt.Rows.Count -1)

{

//Remove the Selected Row data

dt.Rows.Remove(dt.Rows[rowID]);

ResetRowID(dt);

}

}

//Store the current data in ViewState for future reference

ViewState["CurrentTable"] = dt;

//Re bind the GridView for the updated data

Gridview1.DataSource = dt;

Gridview1.DataBind();

}






Left by Aravindh on Aug 11, 2011 5:57 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
@Vinz,
Thank You for Your Guidance.
to solve my problem,
1) i modified the condition like,
if i<dt.rows.count (instead of giving 'if i<dt.rows.count-1 ) which is in the function SetPreviousData()
2)and also i added few lines in LinkButton1_Click() like,
.
.
if (ViewState["CurrentTable"] != null)

{

DataTable dt = (DataTable)ViewState["CurrentTable"];
'after the above said line , i have added the following one

ViewState["CurrentTable"] = dt;


for (int i = 0; i < dt.Rows.Count - 1; i++)
{
'extract the textbox and dropdown values
}

ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data

Gridview1.DataSource = dt;

Gridview1.DataBind();
}


}

if i did like the above said... my problem is solved...
is this right way?
thanks in advance...
if (dt.Rows.Count > 1)
{...so and so}






Left by Aravindh on Aug 11, 2011 6:04 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
the last post of mine is the right one..to solve my problem. the previous of the last one is not the right one. by mistakenly uploded it. i did it in vb.net . dnt know the above said code conversion into c# is right one or not.
Thanks to the author for the nice post ....
Left by Aravindh on Aug 12, 2011 5:07 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Aravindh,

Can you please your code which is in vb.net because i am facing the same problem as you are and could figure it out. Thanks in advance...
Left by Swetha on Sep 16, 2011 1:06 AM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
ravindh,

Can you please upload your code which is in vb.net because i am facing the same problem as you are and could not figure it out. Thanks in advance... This will help me a lot
Left by Swetha on Sep 16, 2011 1:07 AM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Hi,

After deleting the row as you have send I am loosing the data on the other rows.
Left by rekha on Oct 05, 2011 1:50 AM

# want to delete the row from the gridview reference to its serial number.its not connect to the database..
Requesting Gravatar...
hiii...Plz help me...I m searching for this code for 3 days...
I cant delete the rows from the gridview...its binding with the data table..and generate serial number...so according to it i want to delete the rows from gridview...
Left by Jyoti on Nov 02, 2011 9:24 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Hi Jyoti,

if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{

for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
if (i == e.RowIndex)
{
dtCurrentTable.Rows[e.RowIndex].Delete();
break;
}
}
gvPrevEmp.DataSource = dtCurrentTable;
gvPrevEmp.DataBind();

ViewState["CurrentTable"] = dtCurrentTable;
}
}
else
{
Response.Write("ViewState is null");
}
Left by Manoj on Nov 03, 2011 5:28 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Thanks for vinz for your delete logic but there is a flaw in your logic that if you delete from the middle of grid then all the data below it is cleared, so the better way to do is allow the user to do delete from bottom to up,but still there is a bug of data from last row is cleared.
Left by Sumant on Nov 29, 2011 2:08 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
hi.. when i delete a row it resets the all the data in the rows i have not deleted
Left by Anthony on Dec 01, 2011 2:28 PM

# re: UPDATED: Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
Requesting Gravatar...
Thanks a lot! I'm having a peculiar issue though... I have RequiredFieldValidators to each TextBox. So when the user tries to post after accidentally adding another row, they cannot submit the form. Any way around this?
Left by Aaron on Jun 05, 2012 7:57 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net | Join free