应有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();//显示价格
                }
            }
        }
    }
}

图中的控件如图所示:


 

转载于:https://www.cnblogs.com/leijiangtao/p/4174577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值