应有dataGridView控件

using System.Data.SqlClient;



namespace UseDataGridView

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        string strCon = "Server=localhost;User Id=sa;Pwd=;DataBase=my";//定义数据库连接字符串

        SqlConnection sqlcon;//声明数据库连接对象

        SqlDataAdapter sqlda;//声明数据库桥接器对象

        DataSet myds;//声明数据集对象

        private void Form1_Load(object sender, EventArgs e)

        {

            dataGridView1.AllowUserToAddRows = false;//禁止添加行

            dataGridView1.AllowUserToDeleteRows = false;//禁止删除行

            sqlcon = new SqlConnection(strCon);//实例化数据库连接对象

            sqlda = new SqlDataAdapter("select * from S", sqlcon);//实例化数据库桥接器对象

            myds = new DataSet();//实例化数据集对象

            sqlda.Fill(myds);//填充数据集

            dataGridView1.DataSource = myds.Tables[0];//为dataGridView1指定数据源

            //禁用DataGridView控件的排序功能

            for (int i = 0; i < dataGridView1.Columns.Count; i++)

                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;

            //设置SelectionMode属性为FullRowSelect使控件能够整行选择

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            //设置DataGridView控件中的数据以各行换色的形式显示

            foreach (DataGridViewRow dgvRow in dataGridView1.Rows)//遍历所有行

            {

                if (dgvRow.Index % 2 == 0)//判断是否是偶数行

                {

                    //设置偶数行颜色

                    dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightSalmon;

                }

                else//奇数行

                {

                    //设置奇数行颜色

                    dataGridView1.Rows[dgvRow.Index].DefaultCellStyle.BackColor = Color.LightPink;

                }

            }

            dataGridView1.ReadOnly = true;//设置dataGridView1控件的ReadOnly属性,使其为只读

            //设置dataGridView1控件的DefaultCellStyle.SelectionBackColor属性,使选中行颜色变色

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue;

        }



        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            if (e.RowIndex >= 0)//判断选中行的索引是否大于0,书上这儿错了,应该还要等于

            {



                string sno = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//记录选中的SNo号

                sqlcon = new SqlConnection(strCon);//实例化数据库连接对象

                sqlda = new SqlDataAdapter("select * from S",sqlcon);//实例化数据库桥接器对象 where SNo='"+sno+"

                myds = new DataSet();//实例化数据集对象

                sqlda.Fill(myds);//填充数据集中

                if (myds.Tables[0].Rows.Count > 0)//判断数据集中是否有记录

                {

                    textBox1.Text = myds.Tables[0].Rows[e.RowIndex][0].ToString();//显示版本

                    textBox2.Text = myds.Tables[0].Rows[e.RowIndex][1].ToString();//显示价格

                }

            }

        }

    }

}
应有dataGridView控件
图中的控件如图所示:
应有dataGridView控件
应有dataGridView控件

 

你可能感兴趣的:(datagridview)