马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

DataGridView控件的使用方法,结果异常!请指教!

  [复制链接]
查看3679 | 回复4 | 2013-3-26 17:24:18 | 显示全部楼层 |阅读模式
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string connectionString = "Data Source=.;Initial Catalog=temdp;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connectionString);
        SqlDataAdapter Adapter;
        DataSet dataSet = new DataSet();
        private void Form1_Load(object sender, EventArgs e)
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            string selectString = "select * from tb_DataGridView_2";
            Adapter = new SqlDataAdapter(selectString, conn);
            Adapter.Fill(dataSet);
            dataGridView1.DataSource = dataSet.Tables[0];
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if(e.ColumnIndex==2&&e.RowIndex!=-1||e.ColumnIndex==3&&e.RowIndex!=-1
                ||e.ColumnIndex==4&&e.RowIndex!=-1)
            {
                Brush Bsh_1 = new SolidBrush(dataGridView1.GridColor);
                SolidBrush GLBSH = new SolidBrush(e.CellStyle.BackColor);
                using (Pen datagridLinePen = new Pen(Bsh_1))
                {
                    e.Graphics.FillRectangle(GLBSH, e.ClipBounds);
                    if (e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].
                        Cells[e.ColumnIndex].Value != null &&dataGridView1.Rows[e.RowIndex + 1].Cells[e.
                        ColumnIndex].Value.ToString() !=e.Value.ToString())
                    {
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                            e.CellBounds.Right, e.CellBounds.Bottom - 1);
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
                            e.CellBounds.Right - 1, e.CellBounds.Bottom);
                    }
                    else
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right-1, e.CellBounds.Top,
                            e.CellBounds.Right-1, e.CellBounds.Bottom );   
                    if (e.RowIndex == dataGridView1.Rows.Count - 1)
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                            e.CellBounds.Right, e.CellBounds.Bottom - 1);                  
                    if (e.Value != null)
                        if(!(e.RowIndex>0&&dataGridView1.Rows[e.RowIndex-1].Cells[e.ColumnIndex].Value.
                            ToString()==e.Value.ToString()))
                            e.Graphics.DrawString(e.Value.ToString(),e.CellStyle.Font,Brushes.Black,e.
                                CellBounds.X+2,e.CellBounds.Y+5,StringFormat.GenericDefault);
                    e.Handled = true;
                }
            }
        }
    }

这个是DataGridView控件的使用(书上学的),作用是合并DataGridView控件中的单元格,在SQL中创建一张表格并且显示在DataGridView控件上,有相关的代码相信各位大蛇呢能够看懂。。。之所以发上论坛!是因为虽然这个没有逻辑错误。。。但是在运行之后会出现看不见表格的情况,简单的说就是一种异常,有时间的可以帮我看一下。。。
ibcadmin | 2013-3-26 20:25:21 | 显示全部楼层
在窗体加载事件中绑定,你的事件又错了
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
IoveBC | 2013-4-2 22:15:54 | 显示全部楼层
现在才看见这个回复这个是书上的,事件什么的我知道在哪里,但是绑定我就不知道你在说什么了。。。还有就是请问我抄袭书上的这个代码哪里错掉了?
chao2332601 | 2013-6-16 00:28:59 | 显示全部楼层
谢谢分享!!!
chao2332601 | 2013-6-16 04:43:42 | 显示全部楼层
谢谢分享!!!
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则