从列表中选择一个项目,网格视图中的数据将基于该项目进行更改 [英] select an item from the list based on which the data in the gridview changes

查看:122
本文介绍了从列表中选择一个项目,网格视图中的数据将基于该项目进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我正在使用Windows窗体应用程序.

当从列表中选择特定项目时,我想在gridview中显示表的内容.

我该怎么做?

Hey,

I am working with windows form applications.

I would like to display a the contents of a table in a gridview when a particular item from a list is selected.

how do i go about doing this?

namespace final2
{
    public partial class Form1 : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private DataSet dataset;
        public DataGridView dg;


        public Form1()
        {
            InitializeComponent();

            con = new SqlConnection();
            command = con.CreateCommand();
            con.ConnectionString = "Data Source=CASSINI-003-PC;Initial Catalog=studentdb;Integrated Security=True";
            adapter = new SqlDataAdapter(command);
            dataset = new DataSet();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno", listBox1.SelectedItem);
            command.CommandText = "INSERT into details" + "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }

            ClearFields();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.CommandText = "SELECT * FROM details WHERE StudentID=@StudentID";

            dataset.Tables.Clear();

            int result = adapter.Fill(dataset, "details");

            if (result > 0)
            {
                DataRow srow = dataset.Tables["details"].Rows[0];
                textBox1.Text = srow["StudentID"].ToString();
                textBox2.Text = srow["Name"].ToString();
                textBox3.Text = srow["Age"].ToString();
                textBox4.Text = srow["Gender"].ToString();
                listBox1.SelectedItem = srow["Courseno"].ToString();

            }
            else
            {
                MessageBox.Show("Student does not exist");
            }
       
        }

        void ClearFields()
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currentitem = listBox1.SelectedItem.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            String query = "SELECT * FROM results WHERE Courseno=''"+listBox1.SelectedValue+"''";

            con.Open();

            adapter = new SqlDataAdapter(query, con);
            adapter.Fill(dataset);
            dataGridView1.DataSource = dataset;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the ''studentdbDataSet1.result'' table. You can move, or remove it, as needed.
            this.resultTableAdapter.Fill(this.studentdbDataSet1.result);

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the ''studentdbDataSet2.results'' table. You can move, or remove it, as needed.
            this.resultsTableAdapter.Fill(this.studentdbDataSet2.results);

        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultsTableAdapter.FillBy(this.studentdbDataSet2.results);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.resultsTableAdapter.FillBy1(this.studentdbDataSet2.results);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        
    }
}

推荐答案

只需编写以下内容:

Just Write This :

private void button3_Click(object sender, EventArgs e)
       {

           String query = "SELECT * FROM results WHERE Courseno='"+listBox1.SelectedValue+"'";

           con.Open();

           adapter = new SqlDataAdapter(query, con);
           adapter.Fill(dataset);
           dataGridView1.DataSource = dataset;
           dataGridView1.Databind();
       }


如果您的描述正确,很显然您的查询正在执行,没有错误.问题可能出在该命令的表中,因为它会正确显示表格列.
if your description is correct,it is clear that your query is executing with out errors.The problem can be there may be no values in the table for this command since it shows the table columns correctly.
String query = "SELECT * FROM results WHERE Courseno='"+listBox1.SelectedValue+"'";



确保零件listBox1.SelectedValue在调试模式下返回真实值.



Make sure that the part listBox1.SelectedValue returns a genuine value in the debug mode.


这篇关于从列表中选择一个项目,网格视图中的数据将基于该项目进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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