zoukankan      html  css  js  c++  java
  • DataGridView控件使用Demo

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Data.Common;
    
    namespace DataGridViewDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            string conStr = "server=localhost;database=db_EMS;integrated security=true";
            SqlConnection conn;
            SqlDataAdapter da;
            DataSet ds;
            private void Form1_Load(object sender, EventArgs e)
            {
                //禁止添加行
                dataGridView1.AllowUserToAddRows = false;
                //禁止刪除行
                dataGridView1.AllowUserToDeleteRows = false;
                conn = new SqlConnection(conStr);
                da = new SqlDataAdapter("select * from tb_pdic", conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
                //禁止對列進行排序(這個要在賦值datasource後)
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                    dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                //選中時選中整行
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                //不同的行賦值不同的顏色
                foreach(DataGridViewRow dgv in dataGridView1.Rows)
                {
                    if(dgv.Index%2==0)
                    {
                        dataGridView1.Rows[dgv.Index].DefaultCellStyle.BackColor = Color.LightSalmon;
                    }
                    else
                    {
                        dataGridView1.Rows[dgv.Index].DefaultCellStyle.BackColor = Color.LightPink;
                    }
                }
                dataGridView1.ReadOnly = true;
                //設置選中行的顏色
                dataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue;
    
    
                
    
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                int id =(int) dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                conn = new SqlConnection(conStr);
                da = new SqlDataAdapter("select * from tb_pdic where id=" + id, conn);
                ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    textBox1.Text = ds.Tables[0].Rows[0]["name"].ToString();
                    textBox2.Text = ds.Tables[0].Rows[0]["money"].ToString();
                }
            }
        }
    }
  • 相关阅读:
    托词坚持了170多天,昨天因为晚上打球竟然给忘了
    2013转眼间半年过去了,回顾一下。也看一下计划的实施情况以及下半年的计划
    开始新的板子PCB绘制了。
    致时代前行者:致敬每一个奔腾不息的心灵(转)
    刚才看了年初的计划,增加一部分内容
    五种男人
    哪些行业会用到乐泰胶水?
    第一个python小程序
    一个简单的IPmsg程序源码分析(一)
    关于linux下面printf函数缓冲区问题
  • 原文地址:https://www.cnblogs.com/monkey6/p/12218805.html
Copyright © 2011-2022 走看看