Live2D 看板娘 / Demo

winfrom DataGridView 总结

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 { }
        }

 

posted @ 2016-05-07 09:32  MChuang  阅读(1185)  评论(0编辑  收藏  举报