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

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();

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();

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

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();

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...:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.