DataGridView中的数据在文本中显示
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
DataGridView中的数据在文本中显示
作者:逆命之心
实现汽车信息的查询和编辑,可根据输入的查询条件进行查询,也可以仅输入部分条件。单击“汽车基本信息”中的汽车信息,将在“汽车详细信息”中显示汽车的详细信息
1.在数据库中建一个表CaressInfo
createtable CarsInfo
(
CarId intprimarykeyidentity(1,1),
Brand varchar(50)notnull,--品牌
Typevarchar(50)notnull,--型号
Dischaarge numeric(18,1)notnull,--排量
GearBox varchar(50)notnull check(GearBox='手动'or GearBox='自动'or GearBox='手自一体'),--变速箱
oilUse numeric(18,1)notnull,--理论耗油
Frice int notnull--报价
)
2.创建窗体
变速箱里的内容为:不限,自动,手动,手自一体
3.代码
//DataSet实例化,声明SqlDataAdapter类型的dsa
DataSet da = new DataSet();
SqlDataAdapter dsa;
//声明Select方法用于查询
publicvoid Select()
{
this.da.Clear();
//品牌文本框(txtBrand),排量文本框(txtDischaarge),变速箱文本框(cmbGearBox)
string str = this.txtBrand.Text.Trim();
string str1=this.txtDischaarge.Text.Trim();
string str2 = this.cmbGearBox.Text.Trim();
//判断各种查询条件
if (!str.Equals("") && !str1.Equals("") && !str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Brand='" + str + "' and Dischaarge='" + str1 + "' and GearBox='" + str2 + "'";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
if (!str.Equals("") && str1.Equals("") && str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Brand='" + str + "' ";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
if (str.Equals("") && !str1.Equals("") && str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Dischaarge='" + str1 + "'";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
if (str.Equals("") && str1.Equals("") && !str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where GearBox='" + str2 + "'";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
if (!str.Equals("") && !str1.Equals("") && str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Brand='" + str + "' and Dischaarge='" + str1 + "' ";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
if (!str.Equals("") && str1.Equals("") && !str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Brand='" + str + "' and GearBox='" + str2 + "'";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
if (str.Equals("") && !str1.Equals("") && !str2.Equals(""))
{
string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Dischaarge='" + str1 + "' and GearBox='" + str2 + "'";
dsa = new SqlDataAdapter(Sql, Dbhpler.con);
dsa.Fill(da, "CarsInfo");
this.dgv.DataSource = da.Tables["CarsInfo"];
}
}
privatevoid btnOk_Click(object sender, EventArgs e)
{
//调用Select方法
Select();
}
//DataGridView的CellClick事件
privatevoid dgv_CellClick(object sender, DataGridViewCellEventArgs e) {
//当前行
int index = e.RowIndex;
DataTable table = (DataTable)this.dgv.DataSource;
//在文本框中加载所选中数据的信息
this.txtBrand1.Text = table.Rows[index]["Brand"].ToString();
this.txtDischaarge1.Text = table.Rows[index]["Dischaarge"].ToString(); this.txtFrice.Text = table.Rows[index]["Frice"].ToString();
this.txtType.Text = table.Rows[index]["Type"].ToString();