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());
                }
            }
  • 相关阅读:
    面向 部分
    并发 编程
    数据库 部分
    匿名函数
    Linux 30岁,这些年经历了什么?
    漫谈 HTTP 连接
    华为交换机命令基础入门学习,小白也能看得懂!
    一文讲透APaaS平台是什么
    什么是边缘CDN和虚拟CDN (vCDN)?
    systemd进程管理工具实战教程
  • 原文地址:https://www.cnblogs.com/luoyanghao/p/6082596.html
Copyright © 2011-2022 走看看