日期:2014-05-18 浏览次数:21120 次
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
public class Form1 : System.Windows.Forms.Form
{
    private DataGridView dataGridView1 = new DataGridView();
    private BindingSource bindingSource1 = new BindingSource();
    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;
        this.Controls.Add(dataGridView1);
        InitializeDataGridView();
    }
    private void InitializeDataGridView()
    {
        try
        {
            // Set up the DataGridView.
            dataGridView1.Dock = DockStyle.Fill;
            // Automatically generate the DataGridView columns.
            dataGridView1.AutoGenerateColumns = true;
            // Set up the data source.
            bindingSource1.DataSource = GetData("Select * From Products");
            dataGridView1.DataSource = bindingSource1;
            // Automatically resize the visible rows.
            dataGridView1.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            // Set the DataGridView control's border.
            dataGridView1.BorderStyle = BorderStyle.Fixed3D;
            // Put the cells in edit mode when user enters them.
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
        }
        catch (SqlException)
        {
            MessageBox.Show("To run this sample replace connection.ConnectionString" +
                " with a valid connection string to a Northwind" +
                " database accessible to your system.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            System.Threading.Thread.CurrentThread.Abort();
        }
    }
    private static DataTable GetData(string sqlCommand)
    {
        string connectionString = "Integrated Security=SSPI;" +
            "Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";
        SqlConnection northwindConnection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = command;
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        adapter.Fill(table);
        return table;
    }
    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}
------解决方案--------------------
绑定不变,选中后,先清空原来绑定表,查询加载数据到绑定表即可。
------解决方案--------------------
可以把abc放到一个listbox控件里,
点击触发changge事件。在事件里写查询语句,
------解决方案--------------------
//比如你用radiobutton,设置好这三个单选项,可以这样写:
string conn = "data source=.;initial catalog=capucivar;user id=sa;pwd="; 
SqlConnection mycon = new SqlConnection(conn);
mycon.Open();
string selery;
if (radiobutton1 == true) //条件选项1
 {
    selery = "SQL语句B";
 }
 if (radiobutton2 == true) //条件选项2
 {
    selery = "SQL语句B";
 }
 if (radiobutton3 == true) //条件选项3
 {
     selery = "SQL语句C";
 }
 SqlDataAdapter SQLDA = new SqlDataAdapter(selery, conn);
 DataSet DS = new DataSet();
 SQLDA.Fill(DS);
 this.datagridview1.DataSource = DS.Tables[0].DefaultView; //绑定显示你要的datagridview表;