基于特定文本框在 DataGridView 中显示数据

Showing data in DataGridView based on specific text box

我有一个 C# 程序可以记录用户的作业,并将记录的作业发送到基于服务的数据库。 我能够毫无问题地在 DataGridView 中显示我的数据,但是数据库中的每个项目都分配给了一个用户名。我希望登录到应用程序的人仅查看他们在数据库中记录的作业。

那么,是否可以根据存储在文本框中的用户名显示 table 中的数据?用户名存储在 txtUsername.Text;

这是我必须在 table 中显示数据的代码。

private void DisplayData()
    {
        string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\listm\Documents\Visual Studio 2013\Projects\EnviroWaste Job Logger\EnviroWaste Job Logger\UsersDatabase.mdf;Integrated Security=True";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM LoggedJobs", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        try
                        {
                            sda.Fill(dt);
                            //Set AutoGenerateColumns False
                            tblLoggedJobs.AutoGenerateColumns = true;

                            //Set Columns Count
                            tblLoggedJobs.ColumnCount = 7;

                            //Add Columns
                            tblLoggedJobs.Columns[0].Name = "JobID";
                            tblLoggedJobs.Columns[0].HeaderText = "Job ID";
                            tblLoggedJobs.Columns[0].DataPropertyName = "JobID";

                            tblLoggedJobs.Columns[1].Name = "JobDate";
                            tblLoggedJobs.Columns[1].HeaderText = "JobDate";
                            tblLoggedJobs.Columns[1].DataPropertyName = "JobDate";

                            tblLoggedJobs.Columns[2].Name = "UserID";
                            tblLoggedJobs.Columns[2].HeaderText = "User ID";
                            tblLoggedJobs.Columns[2].DataPropertyName = "UserID";

                            tblLoggedJobs.Columns[3].Name = "IssueSubject";
                            tblLoggedJobs.Columns[3].HeaderText = "IssueSubject";
                            tblLoggedJobs.Columns[3].DataPropertyName = "IssueSubject";

                            tblLoggedJobs.Columns[4].Name = "Screen";
                            tblLoggedJobs.Columns[4].HeaderText = "Screen";
                            tblLoggedJobs.Columns[4].DataPropertyName = "Screen";

                            tblLoggedJobs.Columns[5].Name = "FurtherInformation";
                            tblLoggedJobs.Columns[5].HeaderText = "FurtherInformation";
                            tblLoggedJobs.Columns[5].DataPropertyName = "FurtherInformation";

                            tblLoggedJobs.Columns[6].Name = "JobStatus";
                            tblLoggedJobs.Columns[6].HeaderText = "JobStatus";
                            tblLoggedJobs.Columns[6].DataPropertyName = "JobStatus";

                            tblLoggedJobs.DataSource = dt;
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Not working");
                        }

                    }
                }
            }
        }
    }

首先,根据我的观点,您不必创建单独的管理员 table 来管理管理员.. 如果您的系统有多个管理员.. 对您的用户进行分类!!您可以在您的用户 table

中管理管理员

你的问题的答案是: 字符串查询="Select userId from TableUSer where userName= {0}",textbox.text;

connecion=new connection     
sql command=new command(query,connection);

using (sqldatareader reader=command.executerreader)
while(reader.read())
{

 int userId=reader.getint32(0)

 }
connection.close();

现在将此 userId 传递给您登录的 table 查询以获取用户 JObs

我设法使用 C# 参数解决了我的问题。 感谢所有发布答案的人。

public void DisplayData()
    {
        string constring = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\listm\Documents\Visual Studio 2013\Projects\EnviroWaste Job Logger\EnviroWaste Job Logger\UsersDatabase.mdf;Integrated Security=True";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM LoggedJobs WHERE (UserID = @userID)", con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@UserID", txtUserID.Text);
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        try
                        {
                            sda.Fill(dt);
                            //Set AutoGenerateColumns False
                            tblLoggedJobs.AutoGenerateColumns = true;

                            //Set Columns Count
                            tblLoggedJobs.ColumnCount = 7;

                            //Add Columns
                            tblLoggedJobs.Columns[0].Name = "JobID";
                            tblLoggedJobs.Columns[0].HeaderText = "Job ID";
                            tblLoggedJobs.Columns[0].DataPropertyName = "JobID";

                            tblLoggedJobs.Columns[1].Name = "JobDate";
                            tblLoggedJobs.Columns[1].HeaderText = "JobDate";
                            tblLoggedJobs.Columns[1].DataPropertyName = "JobDate";

                            tblLoggedJobs.Columns[2].Name = "UserID";
                            tblLoggedJobs.Columns[2].HeaderText = "User ID";
                            tblLoggedJobs.Columns[2].DataPropertyName = "UserID";

                            tblLoggedJobs.Columns[3].Name = "IssueSubject";
                            tblLoggedJobs.Columns[3].HeaderText = "IssueSubject";
                            tblLoggedJobs.Columns[3].DataPropertyName = "IssueSubject";

                            tblLoggedJobs.Columns[4].Name = "Screen";
                            tblLoggedJobs.Columns[4].HeaderText = "Screen";
                            tblLoggedJobs.Columns[4].DataPropertyName = "Screen";

                            tblLoggedJobs.Columns[5].Name = "FurtherInformation";
                            tblLoggedJobs.Columns[5].HeaderText = "FurtherInformation";
                            tblLoggedJobs.Columns[5].DataPropertyName = "FurtherInformation";

                            tblLoggedJobs.Columns[6].Name = "JobStatus";
                            tblLoggedJobs.Columns[6].HeaderText = "JobStatus";
                            tblLoggedJobs.Columns[6].DataPropertyName = "JobStatus";

                            tblLoggedJobs.DataSource = dt;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.StackTrace);
                            MessageBox.Show("Not working");
                        }

                    }
                }
            }
        }
    }