Winforms组datagridview值和checkboxlist

时间:2015-02-19 19:56:23

标签: c# winforms datagridview

我有一个包含4列的datagridview:UserID,FirstName,LastName,Email。从SQL数据库中检索数据。我目前有10张这张桌子的记录。我还有一个带有一长串值(角色)的清单框。

我的目标:
根据从datagridview中选择的记录/行,将从列表中检查某些复选框。例如,一个UserID可能标记了第5,第10和第12个角色复选框,而另一个可能是管理员的UserID将检查前10个。在任何情况下,整个清单框都会显示,只需选中不同的框。如果我退出并重新进入,我应该看到相同的值。为了看看它是否让生活更轻松,我有一个文本框,根据我选择的datagridview行显示UserID,我想也许可以将它与checkboxlist链接。但我认为正确的方法是使用datagrid值。

我该怎么做?

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SampleDataGrid
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlDataAdapter adap;
        DataSet ds;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Search";
            button1.Text = "Search";
            try
            {
                con = new SqlConnection();
                con.ConnectionString = "Data Source=My Data Source";
                con.Open();
                adap = new SqlDataAdapter("select RowID as 'ID',UserID as 'User ID', FirstName as 'First Name', LastName as 'Last Name', email as 'E-mail' from JoshTestTable", con);
                ds = new System.Data.DataSet();
                adap.Fill(ds, "User_Details");
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Columns[0].Visible = false;
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                var cmbl = new SqlCommandBuilder(adap);
                adap.Update(ds, "User_Details");
                MessageBox.Show("Successfully Updated.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
 try
            {
                con = new SqlConnection();
                con.ConnectionString = "My Connection String";
                con.Open();
                adap = new SqlDataAdapter("SELECT JoshRoleTable.RoleID,JoshRoleTable.RoleName FROM Josh_REL_Table INNER JOIN JoshRoleTable ON Josh_REL_Table.RoleID = JoshroleTable.RoleID WHERE JoshTestTable.UserID like '%" + textBox2.Text.Trim() + "%' ", con);
                ds = new System.Data.DataSet();
                adap.Fill(ds, "User_Details");
                dataGridView1.DataSource = ds.Tables[0];
                dataGridView1.Columns[0].Visible = false;

            }

            catch (Exception ex)
            {
                MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

                textBox1.Text = row.Cells["User ID"].Value.ToString();
            }


        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

                textBox1.Text = row.Cells["User ID"].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = "My Data Source";
            SqlDataAdapter sda = new SqlDataAdapter("Select UserID, FirstName,LastName,Email FROM JoshTestTable where UserID like '%" + textBox2.Text.Trim() + "%' OR  FirstName like '%" + textBox2.Text.Trim() + "%' OR  LastName like '%" + textBox2.Text.Trim() + "%' OR  Email like '%" + textBox2.Text.Trim() + "%' ", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;

        }









    }
}

1 个答案:

答案 0 :(得分:0)

听起来像你在数据库中需要一些额外的表来保存"角色"数据。这是一个多对多的关系,所以JoshTestTable目前保存您的用户数据。您将需要另一个包含角色详细信息的表,如下所示:

作用: RoleID(INT) RoleName(VARCHAR)

接下来是一个用于存储关系的表:

JoshTestTable_Role_REL: RelationID(INT) 用户ID(INT) RoleID(INT)

然后在dataGridView1_CellClick方法中,您可以查询这两个新表,以找出该用户拥有的角色。类似的东西:

SELECT T2.RoleID, T2.RoleName

FROM JoshTestTable_Role_REL T1 INNER JOIN角色T2 ON T1.RoleID = T2.RoleID 在哪里T1.UserID = XXX

其中XXX是所选用户的用户ID。

然后循环浏览上述查询返回的记录,并相应地启用/禁用复选框。