C# .NET - create a new row in grid view dynamically

Asked By muthuraman alexander on 09-Sep-11 09:16 AM
hi all i want to create a new row in grid view control dynamically like this



how can i do this
any body help me  for this


thanks in advance
MUTHU
Rohan Dave replied to muthuraman alexander on 09-Sep-11 09:24 AM
is your application is windows base or webbase ?
Jitendra Faye replied to muthuraman alexander on 09-Sep-11 09:25 AM

Follow these steps-

1. design you page like this-

<asp:Button ID="btnGetDetails" runat="server" Text="Button"

onclick="btnGetDetails_Click1" OnClientClick ="return confirmation()"/>

<asp:GridView ID="GridView1" runat="server">

</asp:GridView>

<asp:DropDownList ID="DropDownList1" runat="server"

onselectedindexchanged="DropDownList1_SelectedIndexChanged">

</asp:DropDownList>

2. Write this code and code behind-

protected void btnAdd_Click1(object sender, EventArgs e)

{

DataSet dataSet1 = (DataSet)GridView1.DataSource;

string s = DropDownList1.SelectedItem.ToString() ;

DataRow foundRow = dataSet1.Tables[0].Rows.Find(s);

if (foundRow != null)

{

Label1.Text = "Record already Exist.";

}

else

{

DataRow dr = dataSet1.Tables[0].NewRow();

dataSet1.Tables[0].Rows.Add(dr);

GridView1.DataSource = dataSet1;

GridView1.DataBind();

}

}


Try this and let me know.

muthuraman alexander replied to Rohan Dave on 09-Sep-11 09:25 AM
web base application


thanks in advance
MUTHU
Asked By muthuraman alexander on 09-Sep-11 09:31 AM
hi

actually i wan to insert rows in the intermediate postions
if i click the '+' button it ll create a row next to that row ..
how can  i do this

thanks in advance
MUTHU
Reena Jain replied to muthuraman alexander on 09-Sep-11 10:48 AM
Hi,

/* Add new Row */
dataGridView1.Rows.Add();
   
/* Set the cell values of newly added row*/
int index = dataGridView1.Rows.Count-1;
dataGridView1.Rows[index].Cells[0].Value = "Some value";
dataGridView1.Rows[index].Cells[1].Value = "Some Value2"
Add() method will add Blank Row to Grid after adding it you can change the cell values as you need .
Hope this will help you
muthuraman alexander replied to Reena Jain on 12-Sep-11 01:25 AM
hi
thank u for reply

when use ur code in OnRoWCommand() of my grid view

gridview1.Rows.Add();

it showing error
 "gridView does not contain any definition for Add()"

i need help for this

thanks in advance
Muthu