c# datagridview动态修改列



  String[] names = new String[] { "RealName", "LogTime", "FormName", "EventName", "UserIPAddress", "UserComputerName",
            "CustomerID", };
            int length = names.Length;
            dataGridView1.ColumnCount = length;
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dataGridView1.Columns[i].Name = names[i];
            }

            //dataGridView1.ColumnCount = 2;
            //dataGridView1.Columns[0].HeaderText = "姓名";
            //dataGridView1.Columns[0].Name = "";

            //DataGridViewColumn colu = new DataGridViewColumn();
            //colu.Name = "RealName";
            //colu.HeaderText = "姓名";
            //int i = dataGridView1.Columns.Add(colu); //添加列
            //dataGridView1.Columns[i].HeaderText = ""; //设置列表题
            //设置显示的列名
            dataGridView1.Columns["RealName"].HeaderText = "姓名";
            dataGridView1.Columns["LogTime"].HeaderText = "操作日期";
            //设置列的宽度
            dataGridView1.Columns["LogTime"].Width = 150;
            dataGridView1.Columns["FormName"].HeaderText = "模块名称";
            dataGridView1.Columns["FormName"].Width = 150;
            dataGridView1.Columns["EventName"].HeaderText = "具体操作";
            dataGridView1.Columns["UserIPAddress"].HeaderText = "IP地址";
            dataGridView1.Columns["UserComputerName"].HeaderText = "计算机名称";
            dataGridView1.Columns["UserComputerName"].Width = 150;
            dataGridView1.Columns["CustomerID"].Visible = false;//隐藏某列:

你可能感兴趣的:(c# datagridview动态修改列)