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"
<asp:GridView ID="GridView1" runat="server"> </asp:GridView> <asp:DropDownList ID="DropDownList1" runat="server"
</asp:GridView> <asp:DropDownList ID="DropDownList1" runat="server"
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
</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."; }
Label1.Text = "Record already Exist."; }
}
else
DataRow dr = dataSet1.Tables[0].NewRow(); dataSet1.Tables[0].Rows.Add(dr); GridView1.DataSource = dataSet1; GridView1.DataBind(); } }
dataSet1.Tables[0].Rows.Add(dr);
GridView1.DataSource = dataSet1;
GridView1.DataBind();
Try this and let me know.
/* 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
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"