显示C#winform应用程序表中的数据

时间:2014-07-18 17:23:20

标签: c# sql winforms datagridview

我需要一些帮助,将表格中的数据显示在winform上。通常这可以使用datagridview来完成,但是根据我的理解,datagridvide要么显示表的全部内容,要么我可以使用过滤函数来显示与过滤器匹配的数据。

我目前面临的问题是,我有一个名为book_issued的表,其中包含发给学生的所有书籍的详细信息,每个学生都有一个独特的注册表。

我有一个登录表单,可以在登录时获取学生注册的值,然后将其指向另一个表格,该表格应该显示学生已经签发的所有书籍的列表。

要实现这一点,我正在运行sql查询

"SELECT * FROM book_issued WHERE regid = @regid"

我正在跳过传递参数化值并从datareader读取数据的过程。

我的问题是在表单上显示这些数据,非常感谢一些帮助

与问题无关的愚蠢代码,但ppl会在没有它的情况下对这个问题进行投票

 private void student_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'libDataSet8.issue' table. You can move, or remove it, as needed.
            this.issueTableAdapter1.Fill(this.libDataSet8.issue);
            // TODO: This line of code loads data into the 'libDataSet7.issue' table. You can move, or remove it, as needed.
            //this.issueTableAdapter.Fill(this.libDataSet7.issue);
            //// TODO: This line of code loads data into the 'libDataSet6.books' table. You can move, or remove it, as needed.
            //this.booksTableAdapter.Fill(this.libDataSet6.books);

        }

        private void nameToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.booksTableAdapter.name(this.libDataSet6.books, nameToolStripTextBox.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        //Get Student Details
        private void student_details(string input)
        {
            query = "SELECT * FROM student WHERE regid = @regid";
            string conString = Properties.Settings.Default.libConnectionString;
            using (SqlCeConnection conn = new SqlCeConnection(conString))
            {
                conn.Open();
                using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                {
                    cmd.Parameters.AddWithValue("@regid", input);
                    cmd.ExecuteNonQuery();

                    SqlCeDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        dob = reader["dob"].ToString();
                        sbook = Convert.ToInt32(reader["book"].ToString());
                        fine = Convert.ToDouble(reader["fine"].ToString());
                    }
                    string temp = dob.Substring(5, 4);
                    int years = DateTime.Now.Year - Convert.ToInt32(temp);

                    sage = years;

                }
            }

        }
        //Method to validate user inputs
        private bool ValidateISBNi()
        {
            bool bStatus = true;
            if (isbni.Text == string.Empty)
            {
                errorProvider2.SetError(isbni, "");
                errorProvider1.SetError(isbni, "Please Enter ISBN code");
                bStatus = false;
            }
            else
            {
                 query = "SELECT * FROM books WHERE isbn = @isbn";
                 string conString = Properties.Settings.Default.libConnectionString;
                 using (SqlCeConnection conn = new SqlCeConnection(conString))
                 {
                     conn.Open();
                     using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                     {
                         cmd.Parameters.AddWithValue("@isbn", isbni.Text);
                         cmd.ExecuteNonQuery();

                         SqlCeDataReader reader = cmd.ExecuteReader();

                         while (reader.Read())
                         {
                             counter = 1;
                         }

                     }
                 }
                 if (counter == 1)
                 {
                     errorProvider1.SetError(isbni, "");
                     errorProvider2.SetError(isbni, "Good");
                     counter = 0;
                 }
                 else
                 {
                     errorProvider2.SetError(isbni, "");
                     errorProvider1.SetError(isbni, "Invalid ISBN code");
                     bStatus = false;
                 }
            }
            return bStatus;            
        }

        private bool ValidateISBNr()
        {
            bool bStatus = true;
            if (isbni.Text == string.Empty)
            {
                errorProvider2.SetError(isbnr, "");
                errorProvider1.SetError(isbnr, "Please Enter ISBN code");
                bStatus = false;
            }
            else
            {
                query = "SELECT * FROM issue WHERE isbn = @isbn AND rgid = @rgid";
                string conString = Properties.Settings.Default.libConnectionString;
                using (SqlCeConnection conn = new SqlCeConnection(conString))
                {
                    conn.Open();
                    using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@isbn", isbnr.Text);
                        cmd.Parameters.AddWithValue("@rgid", Variables.regid);
                        cmd.ExecuteNonQuery();

                        SqlCeDataReader reader = cmd.ExecuteReader();

                        while (reader.Read())
                        {
                            counter = 1;
                        }

                    }
                }
                if (counter == 1)
                {
                    errorProvider1.SetError(isbnr, "");
                    errorProvider2.SetError(isbnr, "Good");
                    counter = 0;
                }
                else
                {
                    errorProvider2.SetError(isbni, "");
                    errorProvider1.SetError(isbni, "Invalid ISBN code");
                    bStatus = false;
                }
            }
            return bStatus;            
        }


        private void isbni_Validating(object sender, CancelEventArgs e)
        {
            ValidateISBNi();
        }

        private void isbnr_Validating(object sender, CancelEventArgs e)
        {
            ValidateISBNr();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            bool vissue;
            vissue = ValidateISBNi();
            if (vissue)
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }


    }
}

有一堆随机代码可以帮助ne1吗?我不认为这就是为什么我没有在第一时间发布它

1 个答案:

答案 0 :(得分:1)

这是你的意思吗?

在表单1中,假设您有一个TextBox名称 txtRegStud ,那么form1中的值将传递给form2?你与继承有关。

在form1中,这可能是代码:

form2 f = new frm2();
f.strRegStud = txtRegStud.Text;
f.Show();
this.Dispose();

然后在你的form2中,你需要声明公共字符串以便携带任何其他形式的值:

public string strRegStud;
private void frm2_Load(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\dbname.mdb;");
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            Con.Open();
            command.CommandText = String.Format("SELECT * FROM book_issued WHERE regid ='{0}'", strRegStud);;
            command.Connection = Con;
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            Con.Close();
            Con.Dispose();
            datagridview1.DataSource = dt;

        }
        catch (Exception ex)
        {
            XtraMessageBox.Show(ex.Message);
        }
    }

这就是你在寻找什么?