使用第一个单元格中的值填充Datagridview行 [英] Datagridview row filling using value in first cell
问题描述
嗨..
i在datagridview的第一列有一个组合框,我想根据数据库中组合框中选择的值填充整行。
我尝试了什么:
如何使用组合框的值从表中获取数据datagridview填充其行..
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 ..
推荐答案
var ComboBoxValue =((Combobox)GridView1.Row.FindControl(Combobox1 ))。selectedValue();
如果您的comboBox已填充,例如Name列,那么您必须编写所需的ADO .Net以便访问数据库并进行适当的更改。
使用System.Data;
使用System .Data.sqlClient;
DataTable table = new DataTable();
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
}
这是一个GridView事件,EventArgs变量可能会为您指定正在进行更改的行。
希望这会有所帮助!!
最好的问候
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
这篇关于使用第一个单元格中的值填充Datagridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!