如何从datagridview选择特定数据 [英] how choose specific data from datagridview

查看:64
本文介绍了如何从datagridview选择特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个包含名称,国家和地区邮件的表格.

我想将名称写入TextBox并单击按钮以显示其余数据,然后其余数据国家和邮件"出现在DataGridView上.

请帮忙.



I had a table contain name & country & mail.

I want to write the name into TextBox & click on button to show the rest data then the rest data "country & mail " appear on DataGridView.

Please if you can help me.

推荐答案

正如orc_orc_orc所说,您的问题并不十分清楚.

我的意思是要在TextBox中输入名称,然后在单击Button时,所有具有该名称的记录都显示在DataGridView中.

如果正确,那么您需要类似以下内容的内容,该内容使用参数化查询(有关该表达式的详细信息,请参见Google)
As orc_orc_orc has said, your question is not entirely clear.

I have taken it to mean that you want to enter a name into a TextBox, then when you click a Button all records with that name are displayed in a DataGridView.

If that is correct, then you need something like the following, which uses a Parameterized Query (google for that expression for more information)
 private readonly string connectionString = //PUT YOUR CONNECTION STRING HERE
 // The SLQ to SELECT the data I want to see. NOTE the @first at the end. This is the parameter.
 private readonly string selectString = "SELECT First, Last, Telephone FROM dbo.CONTACTS WHERE First = @first";

 // A DataSet to hold the results of the query
 private DataSet contactsDS = null;

 // The click handler for the button
 private void btnFilter_Click(object sender, EventArgs e)
 {
     // Using the 'using' statement is the safest way to do this.
     using (SqlConnection contactConnection = new SqlConnection(connectionString))
     {
         // Set up the SqlCommand using the selectString from above
         SqlCommand contactsCommand = new SqlCommand(selectString, contactConnection);
         // Tell the SQLCommand that we are using a parameter called @first
         // and that in the database it is an NVarChar of size 20
         contactsCommand.Parameters.Add(new SqlParameter("@first", SqlDbType.NVarChar, 20));
         // Now tell it to get the value of the parameter from our TextBox (txtFirstName)
         contactsCommand.Parameters["@first"].Value = this.txtFirstName.Text;

         // Use an SqlDataAdapter to get the data
         SqlDataAdapter contactsDA = new SqlDataAdapter(contactsCommand);

         // Create a new DataSet each time
         contactsDS = new DataSet("CPContacts");
         // Use the SqlDataAdapter to fill the DataSet
         contactsDA.Fill(contactsDS, "Contacts");

         // Tell the DataGridView where to find it's data.
         dgvResults.DataSource = contactsDS.Tables["Contacts"];
     }
}



希望这会有所帮助.



Hope this helps.


这篇关于如何从datagridview选择特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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