-1

How to add a row dynamically in editable grid in asp.net using C#. In my website I need to take input from user for number of rows required and then generate an editable grid view. Please send me code to solve this.

Thank You.

Incredible
  • 3,495
  • 8
  • 49
  • 77
  • 1
    check if this helps.http://stackoverflow.com/questions/10063770/c-sharp-how-to-add-a-new-row-to-datagridview-programmatically – samar Mar 21 '14 at 05:57
  • and this link http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows(v=vs.110).aspx – samar Mar 21 '14 at 05:59
  • and if it is asp.net then try this link http://stackoverflow.com/questions/19485909/add-new-row-in-gridview-after-binding-c-asp-net – samar Mar 21 '14 at 06:00
  • 2
    What have you tried? You must try from your end and show where you are stuck. – Incredible Mar 21 '14 at 06:02

2 Answers2

1

I have solved my question and got the correct answer through self study. I want to share it..

First add a DropDownList and a GridView in your form and in DropDownList add some choices for user regarding number of rows they want to add at run time, in my website I have added four numbers : 1,2,3,4; And the code behind is:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
   int x = Convert.ToInt32(DropDownList1.SelectedItem.ToString()); // Number of rows
   DataTable dt = new DataTable();
   for (int i = 0; i < x; i++)
   {
     DataRow row1 = dt.NewRow();
     dt.Rows.Add(row1);    
   }

   for (int i = 0; i < x; i++)
   {
     DataRow row1 = dt.NewRow(); //Adding rows
     dt.Rows.Add(row1);
   }

   //Bind the datatable with the GridView.
   GridView1.DataSource = dt;
   GridView1.DataBind();
}
Kevin
  • 4,798
  • 19
  • 73
  • 120
0

You have to build a datasource which you can bind to the Grid. For example you can use a DataTable or List<myObject> to store your data. Then use this as datasource and the GridView become show datas from you datasource. If you want to add/delete datas just add/delete the object from your datasource. Example code:

public class Test{
 public int Id {get; private set;}
 public string Bez {get; private set;}

 public Test(int id, string bez)
 {
   this.Id = id;
   this.Bez = bez;
 }
}

Create Datasource with objects:

List<Test> allTests = new List<Test>();
allTests.add(new Test(1, "Test"));
allTests.add(new Test(2, "Test2"));

myGrid.DataSource = allTests;

Now you can just work with the list. The Grid will need Refresh maybe to show edits. If you edit data in your Grid this will directly effect your datasource objects.

If this is not what you are searching for, clarify you question please.

Sebi
  • 3,879
  • 2
  • 35
  • 62
  • At form load time my editable grid shows only one row and I want to allow user to select how many rows he wants to enter through a drop down list and after selection that much row should be displayed in editable grid please suggest me how I can solve this – user3422771 Mar 22 '14 at 04:18
  • 1
    You need to transfer the selected rows from the Dropdown list to datasource of your Grid. It would be helpfull if you edit your question with the code you have done so far. Then i can give you closer help maybe. – Sebi Mar 25 '14 at 07:53