码迷,mamicode.com
首页 > Windows程序 > 详细

winfrom DataGridView 总结

时间:2016-05-07 13:12:31      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:

1. 想让上面的每一列都自动调整并塞满整个控件

  技术分享

 

2. 增加序列号

  在RowPostPaint事件中,添加如下代码

  

     SolidBrush b = new SolidBrush(this.dgvleft.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dgvleft.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
     

3. 将DataGridView的数据转成DataTable函数

  

public DataTable GetDgvToTable(DataGridView dgv)
        {
            DataTable dt = new DataTable();

            // 列强制转换
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                dt.Columns.Add(dc);
            }

            // 循环行
            for (int count = 0; count < dgv.Rows.Count; count++)
            {
                DataRow dr = dt.NewRow();
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

4. 鼠标上事件

  

    private void Ups()
        {
            try
            {
                dgvright.Rows.Clear();
                int nowrow = dgvleft.CurrentRow.Index;
                int prvrow;
                if (nowrow > 0)
                {
                    prvrow = nowrow - 1;
                    dgvleft.CurrentCell = dgvleft.Rows[prvrow].Cells[0];

                    string value = dgvleft.Rows[prvrow].Cells["put_Order"].Value.ToString();
                    AddLeftData(value);
                }
                else
                {
                    MessageBox.Show("最顶端");
                }
            }
            catch { }

        }

        private void Downs()
        {
            try
            {
                dgvright.Rows.Clear();
                int nowrow = dgvleft.CurrentRow.Index;
                int prvrow;
                if (nowrow < dgvleft.Rows.Count - 1)
                {
                    prvrow = nowrow + 1;
                    dgvleft.CurrentCell = dgvleft.Rows[prvrow].Cells[0];

                    object value = dgvleft.Rows[prvrow].Cells["put_Order"].Value;
                    if (value != null)
                    {
                        if (value.ToString() != "")
                        {
                            AddLeftData(value.ToString());
                        }
                    }

                }
                else
                {
                    MessageBox.Show("最后一行");
                }
            }
            catch { }
        }

 

winfrom DataGridView 总结

标签:

原文地址:http://www.cnblogs.com/mchuang/p/5467754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!