zoukankan      html  css  js  c++  java
  • 将sqlserve数据绑定到dataGridView中及一些操作

    一:将数据绑定到dataGridView控件上。

    string sqlconn = "server=.;database=student;integrated security=true";
                try
                {
                    string sqlcom = "select * from student_info";
                    SqlConnection conn = new SqlConnection(sqlconn);
                    SqlDataAdapter da = new SqlDataAdapter(sqlcom, conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;
                    dataGridView1.AllowUserToAddRows = false;
                    conn.Close();
                }    

    二:点击dataGridView的某一行将改行数据对应显示在textBox上。

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                try
                {
                    snoTextBox.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["stu_sno"].Value.ToString();
                    nameTextBox.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["stu_name"].Value.ToString();
                    sexTextBox.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["stu_sex"].Value.ToString();
                    ageTextBox.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["stu_age"].Value.ToString();
                }
                catch (Exception a)
                {
                    MessageBox.Show(a.ToString());
                }
            }
  • 相关阅读:
    JAVA类型之间的转换
    Mysql语句
    Tomcat 优化
    JVM原理及调优
    static
    指针与引用
    sizeof
    遇到问题:c++ 直接cout输出char类型变量地址乱码
    编程过程中全面考虑问题的能力
    表、栈和队列
  • 原文地址:https://www.cnblogs.com/luoyanghao/p/6082596.html
Copyright © 2011-2022 走看看