0

Hello,

i have two controls one Dropdownlist and another a text and i have add button besides them. Both the ddl and textbox r populated from database. I have a gridview placed below these controls.

when a user clicks on add the gridview should populate with the selected value in ddl and the text in textbox in the gridview. If the user does it second time the new value should be appended below the previous value.

i dont want to hit the database with my each successive add clicks.

i want simple functionality just click we add values in listbox. How can we achieve this on Gridview control?

cya,
Rohan

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by rohand
0

Add following code in button's click handler.

DataTable dt=null;
    if(Session["dt"]!=null) {
         dt=new DataTable();
         dt.Columns.Add("Col1");
         dt.Columns.Add("Col2");
         Session["dt"]=dt;
    }
    dt=(DataTable)Session["dt"];
    dt.Rows.Add(TextBox1.Text,DropDownList1.SelectedValue);
   
    GridView1.DataSource=dt;
    GridView1.DataBind();
0

Hi,

Thank you for the code...however i am getting object reference error... Please help...

Line 105: }
Line 106: dt = (DataTable)Session["dt"];
Line 107: dt.Rows.Add(ddlDirectoryAccess.SelectedItem.Text, txtDirRemarks.Text);
Line 108: gvwDAccess.DataSource = dt;
Line 109: gvwDAccess.DataBind();

0

Sorry! I haven't test my code.

DataTable dt=null;
    if(Session["dt"]==null) {
         dt=new DataTable();
         dt.Columns.Add("Col1");
         dt.Columns.Add("Col2");
         Session["dt"]=dt;
    }
    dt=(DataTable)Session["dt"];
    dt.Rows.Add(TextBox1.Text,DropDownList1.SelectedValue);
   
    GridView1.DataSource=dt;
    GridView1.DataBind();

Edited by __avd: Edit

0

hey you need to create NewRow each time when you click on your button. In the code you are not creating newrow and directly adding value in dt.Rows.add() collection and i believe that's why you are getting error.
After creating newrow the code of adatapost will looks like :

DataTable dt=null;
if(Session["dt"]==null) 
{
dt=new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
Session["dt"]=dt;
}
dt=(DataTable)Session["dt"];
DataRow dr = dt.NewRow();
dr("Col1") = TextBox1.Text;
dr("Col2") = DropDownList1.SelectedValue;
dt.Rows.Add(dr);
GridView1.DataSource=dt;
GridView1.DataBind();

try it it will solve your problem :-)

ne help??? its urgent...:(

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.