欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

winform datagridview使用

程序员文章站 2022-06-08 16:04:18
...

 

绑定datatable
DataTable OrigDataDt;       
OrigDataDt = this.dgOrigianlView.DataSource as DataTable;
OrigDataDt = new DataTable();
OrigDataDt.Columns.Add("Column1");
dgOrigianlView.DataSource = OrigDataDt;

DataRow dr = OrigDataDt.NewRow();
dr[0]=Convert.ToString(pDeviceModel.DataVar1, 2);
OrigDataDt.Rows.Add(dr);



 #region【方法:原始数据GRIDVIEW新增行事件响应方法】
 private void dgOrigianlView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
     try
   {
                for (int i = 0; i < e.RowCount; i++)
                {
                    dgOrigianlView.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dgOrigianlView.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
                }
                for (int i = e.RowIndex; i < dgOrigianlView.Rows.Count; i++)
                {
                    dgOrigianlView.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dgOrigianlView.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }
            }
            catch { }
         
        }
        #endregion

        #region【方法:原始数据GRIDVIEW删除行事件响应方法】
        private void dgOrigianlView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            try
            {
                for (int i = 0; i < e.RowCount; i++)
                {
                    dgOrigianlView.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dgOrigianlView.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
                }
                for (int i = e.RowIndex; i < dgOrigianlView.Rows.Count; i++)
                {
                    dgOrigianlView.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    dgOrigianlView.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }
            }
            catch { }
           
        }
        #endregion

 #region【方法集合:列显示控制事件响应方法】
        private void ckbcolum1_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbcolum1.Checked)
            {
                this.dgDataView.Columns[0].Visible = false;
            }
            else
            {
                this.dgDataView.Columns[0].Visible = true;
            }

        }



 //winform自动刷新功能
         int maxcount = this.dgDataView.Rows.Count;
            if (m_autorefresh)
            {
                dgDataView.CurrentCell = dgDataView.Rows[maxcount - 2].Cells[2];
            }


      #region 【设置DATAGRID列颜色】
        private void SetDataViewColor()
        {
            dgDataView.Columns["FOrginalData1"].DefaultCellStyle.BackColor = Color.Aqua;
      }