如何在c#中为不同的用户角色隐藏网格视图列

时间:2018-01-28 14:45:48

标签: c# sql-server

没有错误显示!它没有运作!

我需要在登录" Stock_Controller"时隐藏此UserType的UserID列。并将其显示为"管理员"。

我有一个带有项目列的网格视图,如下所示:

ID|Stock_Type|Stock_No|No_of_pieces|Gem_Type|Weight|Image|Cost|Create_Date|Update_date|UserID|

这是我的登录代码:

try
{
    SqlCommand selectCommand = new SqlCommand(" Select * from New_User where User_Name=@USER_ID and Password=@PASS", conn);
    selectCommand.Parameters.Add(new SqlParameter("USER_ID", txtusername.Text.ToString()));
    String password = "";

    using (SHA1 sha1 = SHA1.Create())
    {
        // sha1.Initialize();
        byte[] data = sha1.ComputeHash(Encoding.UTF8.GetBytes(txtpassword.Text));

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < data.Length; ++i)
        {
            sb.Append(data[i].ToString("x2"));
        }

        password = sb.ToString();
    }

    selectCommand.Parameters.Add(new SqlParameter("PASS", password));
    string UserType = null;

    SqlDataReader reader = selectCommand.ExecuteReader();
    bool rowfound = reader.HasRows;

    if (rowfound)
    {
        while (reader.Read())
        {
            UserType = reader["User_Type"].ToString().Trim();

            if (UserType == "Administrator") 
            {
                GlobalVariablesClass.VariableOne = txtusername.Text;
                MessageBox.Show("Welcome ", "Admin Login", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Admin_Menu frm = new Admin_Menu();
                frm.bunifuFlatButton3.Visible = true;
                frm.Show();

                Stocks_Gems sg = new Stocks_Gems(); /*this is the UserControl: which has the grid-view*/ 
                sg.dataGridView1.Columns[10].Visible = true;  /*i have selected the 10th column which needed to be Visibile*/
                sg.Show();

                this.Hide();
            }
            else if (UserType == "StockController")
            {
                GlobalVariablesClass.VariableOne = txtusername.Text;
                MessageBox.Show("Welcome ", "User Login", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Admin_Menu frm = new Admin_Menu();
                frm.bunifuFlatButton3.Visible = false;
                frm.Show();

                Stocks_Gems sg = new Stocks_Gems(); /*this is the UserControl: which has the grid-view*/ 
                sg.dataGridView1.Columns[10].Visible = false;  /*i have selected the 10th column which needed to be Hidden*/
                sg.Show();

                this.Hide();
            }
        }
    }
    else
    {
        MessageBox.Show(" Invalid User Or Password ", "Login ", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    reader.Close();
}

登录过程 用户登录&gt;然后打开主菜单表格&gt;然后点击按钮STOCK DETAILS&gt; (在右侧网格视图上)是一个USercontrol打开。但它不影响专栏。请帮助!!

1 个答案:

答案 0 :(得分:0)

如果UserType既不是Administrator也不是StockController,那么显示/隐藏列的代码将无法运行,列将保持当前状态,这是最可能Visible=true

假设上述问题不是您的问题,并且保证用户为AdministratorStockController,您可能会发现dataGridView1的列是自动生成的。对于自动生成的列,除DataBindingComplete event外,Visible属性不起作用。请参阅remarks for the Visible Property

  

要隐藏绑定到数据源时自动生成的列,请在DataBindingComplete事件处理程序中设置此属性。

这会使代码工作变得有点复杂。如果将DataGridView.AutoGenerateColumns设置为false并手动指定所有列,则会容易得多。然后你的代码将无需更改即可运行。