Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my table has three columns productid,productname,price

In dropdownlist i want display all productname-price from database.

in dropdown i need like this below

iphone-15000
oneplus-2000

all in gridview column.

What I have tried:

protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         SqlConnection con1 = new SqlConnection(@"user id=sa;password=ssa;database=Mohan;data source=PCTH101\PCTH101");
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
           con1.Open();
             var ddl = (DropDownList)e.Row.FindControl("DropDownList3");
             int productid = Convert.ToInt32(e.Row.Cells[0].Text);
             SqlCommand cmd = new SqlCommand("SELECT * from productinfo1", con1);
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             DataSet ds = new DataSet();
             da.Fill(ds);
             con1.Close();
             ddl.DataSource = ds;
             ddl.DataTextField = "productname-price";
          //   ddl.DataTextField = "price";
             ddl.DataValueField = "productid";
             ddl.DataBind();
             ddl.Items.Insert(0, new ListItem("--Select--", "0"));
         }

     }
Posted
Updated 27-Feb-17 20:39pm
Comments
Graeme_Grant 28-Feb-17 1:19am    
Asp.Core, Asp.Net, Mvc, Winform, Wpf, UWP, or Xamarin? DaGridView, GridView, etc... Please be specific.
GrpSMK 28-Feb-17 1:24am    
Asp.net
GrpSMK 28-Feb-17 1:25am    
Gridview

May be easiest to do in the sql query - join two columns into a display value: sql - concatenate two database columns into one resultset column - Stack Overflow[^]
 
Share this answer
 
Comments
GrpSMK 28-Feb-17 2:01am    
no i need two column values from same table
Graeme_Grant 28-Feb-17 2:05am    
So you don't want to display two DB table columns in a single column in your grid?
change the query
SQL
select  (productname + '-' + CAST( price as nvarchar(50)) ) as 'productname-price' , productid from productinfo1
 
Share this answer
 
Comments
Graeme_Grant 28-Feb-17 2:18am    
Exactly what the link I gave them does.
GrpSMK 28-Feb-17 2:37am    
yea working fine thank you.
Harshal Kale 28-Feb-17 6:41am    
Great!!
This code is working fine....
SqlCommand cmd = new SqlCommand("select productid, productname + ' - ' + price as new from productinfo1", con1);
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             DataSet ds = new DataSet();
             da.Fill(ds);
             con1.Close();
             ddl.DataSource = ds;
             ddl.DataTextField = "new";
             //   ddl.DataTextField = "price";
             ddl.DataValueField = "productid";
             ddl.DataBind();
             ddl.Items.Insert(0, new ListItem("--Select--", "0"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900