从datagridview中查找结果 [英] finding results from datagridview

查看:60
本文介绍了从datagridview中查找结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,如果我使用这些代码来查找结果,那么它不起作用请检查



private void btnadd_Click(object sender,EventArgs e)

{

尝试

{

con = new SqlConnection(Data Source = .; Initial Catalog = DSIIDC2; Integrated Security = True );

con.Open();

string str =select caseno,courtname,casetype,status from addcaasedetails where casetype ='+ casedetails.SelectedIndex.ToString ()+'和caseno ='+ casetypelist.SelectedIndex.ToString()+';



SqlDataAdapter da = new SqlDataAdapter(str,con);

DataSet ds = new DataSet();

da.Fill(ds,addcaasedetails);





dataGridView1.DataSource = ds.Tables [addcaasedetails]。DefaultView;







}

catch(例外情况)

{

MessageBox.Show (ex.Message);

}



}

hello if i use this codes for finding results so it is not working please check

private void btnadd_Click(object sender, EventArgs e)
{
try
{
con = new SqlConnection("Data Source=.;Initial Catalog=DSIIDC2;Integrated Security=True");
con.Open();
string str = "select caseno,courtname,casetype,status from addcaasedetails where casetype='" + casedetails.SelectedIndex.ToString() + "' and caseno='" + casetypelist.SelectedIndex.ToString() + "'";

SqlDataAdapter da = new SqlDataAdapter(str, con);
DataSet ds = new DataSet();
da.Fill(ds,"addcaasedetails");


dataGridView1.DataSource = ds.Tables["addcaasedetails"].DefaultView;



}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

推荐答案

你好,



正如Richard Deeming所说,你首先需要使用参数化查询而不是字符串连接,你的代码很容易被sql注入。



通过查看查询我假设你想 addcaasedetails 表中搜索<​​/ i>记录并将它们绑定到网格控件。



在这里设置DataSource属性,但是你没有绑定它,请尝试如下。如果您的查询返回数据,您应该能够在网格控件中看到它。



Hi,

As Richard Deeming says you first need to use parameterized query instead of string concatenation your code is vulnerable to sql injection.

By looking at query I assume you want to search records from addcaasedetails table and bind them to grid control.

Here you set the DataSource property but you did not bind it, try as below. If your query return you data you should be able to see it in grid control.

dataGridView1.DataSource = ds.Tables["addcaasedetails"].DefaultView;
dataGRidView1.DataBind(); //you are missing this so data is not bind to control





希望这有帮助。



Hope this helps.


这篇关于从datagridview中查找结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆