C# dataGridView1 添加数据 和清空数据

    #region MyRegion

            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();

            DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();

            DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();

            DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();

            DataGridViewTextBoxColumn col4 = new DataGridViewTextBoxColumn();

            DataGridViewImageColumn col5 = new DataGridViewImageColumn();


            DataGridViewTextBoxCell celltext = new DataGridViewTextBoxCell();

            DataGridViewImageCell cellimage = new DataGridViewImageCell();

            col.CellTemplate = col1.CellTemplate = col2.CellTemplate = col3.CellTemplate = col4.CellTemplate = celltext;

            col5.CellTemplate = cellimage;

            col.HeaderText = "名称";

            col1.HeaderText = "分类";

            col2.HeaderText = "数量";

            col3.HeaderText = "价格";

            col4.HeaderText = "销售时间";

            col5.HeaderText = "图片";

            dataGridView1.Columns.Add(col);

            dataGridView1.Columns.Add(col1);

            dataGridView1.Columns.Add(col2);

            dataGridView1.Columns.Add(col3);

            dataGridView1.Columns.Add(col4);

            dataGridView1.Columns.Add(col5);
            //--
            DataGridViewRow row = new DataGridViewRow();

            int index = dataGridView1.Rows.Add(row);

            dataGridView1.Rows[index].Cells[0].Value = "0001";

            dataGridView1.Rows[index].Cells[1].Value = "0002";

            dataGridView1.Rows[index].Cells[2].Value = "0003";

            dataGridView1.Rows[index].Cells[3].Value = "0004";

            dataGridView1.Rows[index].Cells[4].Value = "0005";

            // dataGridView1.Rows[index].Cells[5].Value = "";// Image.FromFile();

            #endregion

 

this.dataGridView1.Columns.Add("0","001");//添加列
this.dataGridView1.Columns.Add("0", "002");
this.dataGridView1.Columns.Add("0", "003");
this.dataGridView1.Rows.Add("第一个字段值", "第二个字段值", "第三个字段值");

 

DataTable dt = (DataTable)dataGridView1.DataSource;
//dt.Rows.Count
//dt.Columns.Count
//if (dt != null&&dt.Rows.Count > 0 )
//{
// dt.Columns.Clear();
// dt.Rows.Clear();
// dataGridView1.DataSource = dt;
//}
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();

posted @ 2018-06-30 11:32  enych  阅读(2726)  评论(0编辑  收藏  举报