Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi ..
i have a combobox in the first column of the datagridview and I want to fill the entire row based on the value selected in the combobox from database .

What I have tried:

how can i fetch the data from table using value of combobox in datagridview to fill its row ..
Posted
Updated 28-Mar-17 22:29pm
Comments
Suvendu Shekhar Giri 29-Mar-17 0:25am    
If you know the value to bind combobox which seems to be your key to find other details of the row, why can't you bring all other columns from database? Isn't it a very simple SELECT statement.
Share what have you tried so far or the relevant code so that we can understand the problem correctly.
Karthik_Mahalingam 29-Mar-17 1:30am    
Suvendu, you have got a reply.
Member 12741312 29-Mar-17 0:48am    
@Suvendu , thanks , but I know how to display all the data in the datagridview using SELECT statement .So question is how can I fill the row in the datagridview keeping the value of first cell freeze and fill remaining cells with data returned from query.
i am using simple fill method to do it , but it is not working.
Karthik_Mahalingam 29-Mar-17 1:30am    
Always use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.

1 solution


var ComboBoxValue = ((Combobox)GridView1.Row.FindControl("Combobox1")).selectedValue();


If your comboBox is populated for example, with the column 'Name' then you have to write the required ADO.Net in order to gain access into the database and make the appropriate changes.

using System.Data;
using System.Data.sqlClient;

DataTable table = new DataTable();


using(sqlConnection connection = new sqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["preffered Connection string"].ConnectionString.toString()))
  {
    connection.Open();
    using(sqlCommand cmd = new sqlCommand(String.Format(@"Insert into tbl_Clients("+ "LASTNAME" + "," + "PHONE" + "," + "CELLPHONE" + ")         Values('{0}','{1}','{2}','{3}') Where Name ={4}",value1,value2,value3,value4,ComboBoxValue.toString()),connection)){
         using(sqlAdapter adapter = new sqlAdapter(cmd) ){
             adapter.fill(table);
         }    
        
      }
  }
 GridView1.Datasource = table;
 GridView1.DataBind();


If you would like to populate a single Row then you have to be specific about it and specify the required Row and cell in order to do so.
OnSelectedIndexChanged(object Sender,EventArgs e){
   var DataRow = e.Row
}


This is an event of GridView of which the EventArgs variable might give you the specified Row that the changes are taking place.

Hope this helps!!
Best Regards
 
Share this answer
 
v3
Comments
Richard Deeming 30-Mar-17 14:55pm    
Also, you have a syntax error in your INSERT statement.

And an INSERT statement doesn't return any rows, so you can't use a SqlDataAdapter to load the results. You should be using ExecuteNonQuery instead.

And your code won't compile, because C# is case-sensitive.
Prifti Constantine 31-Mar-17 2:26am    
Thanks for the information! Ill keep it in mind!

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