好记性不如烂笔头——datagridview相关

DataGridViewTextBoxColumn dgv_IDY = new DataGridViewTextBoxColumn();

            dgv_IDY.Visible = false;

            dgv_IDY.HeaderText = "IDY";

            dgv_IDY.Name = "IDY";

            DataGridViewTextBoxColumn dgv_IDT = new DataGridViewTextBoxColumn();

            dgv_IDT.Visible = false;

            dgv_IDT.HeaderText = "IDT";

            dgv_IDT.Name = "IDT";

            DataGridViewTextBoxColumn dgv_chrPoint = new DataGridViewTextBoxColumn();

            dgv_chrPoint.Visible = false;

            dgv_chrPoint.HeaderText = "chrPoint";

            dgv_chrPoint.Name = "chrPoint";

            DataGridViewTextBoxColumn dgv_chrWorName = new DataGridViewTextBoxColumn();

            dgv_chrWorName.HeaderText = "工作面名称";

            dgv_chrWorName.Name = "chrWorName";

            dgv_chrWorName.ReadOnly = true;

            dgv_chrWorName.Width = 100;

            dgv.Columns.Add(dgv_IDY);

            dgv.Columns.Add(dgv_IDT);

            dgv.Columns.Add(dgv_chrPoint);

            dgv.Columns.Add(dgv_chrWorName);
View Code

 dgv.AllowUserToAddRows = false;//禁用自动增长行
 dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;//

for (int i = 0; i < dgv.Columns.Count; i++)

            {

                 dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;//禁用排序

                 for (int j = 0; j < dgv.Rows.Count; j++)

                 {

                     dgv.Rows[j].Cells[3].Style.BackColor =Color.Azure;

                 }

            }
View Code
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;//禁止改变列头高度

 

void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

        {

            double outDb = 0;

            //可编辑的列

            if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly || e.ColumnIndex == dgv.Columns.Count)

            {

                return;

            }

            if (double.TryParse(e.FormattedValue.ToString(), out outDb) || string.IsNullOrEmpty(e.FormattedValue.ToString()))//如果输入的不是数字

            {

                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;

                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Green;

            }

            else

            {

                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;

            }

        }

 

public static DataTable DgvToDt(DataGridView dv)

        {

            DataTable dt = new DataTable();

            DataColumn dc;

            for (int i = 0; i < dv.Columns.Count; i++)

            {

                dc = new DataColumn();

                dc.ColumnName = dv.Columns[i].Name;

                dt.Columns.Add(dc);

            }

            for (int j = 0; j < dv.Rows.Count; j++)

            {

                DataRow dr = dt.NewRow();

                for (int x = 0; x < dv.Columns.Count; x++)

                {

                    dr[x] = dv.Rows[j].Cells[x].Value;

                }

                dt.Rows.Add(dr);

            }

            return dt;

        }
datagridview转datatable

 

你可能感兴趣的:(datagridview)