Adding new columns to a Winforms DataGridView via code

前端 未结 4 2372
情歌与酒
情歌与酒 2021-02-19 05:43

I\'m trying to add N number of columns for each days of a given month:

var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, month);

for (int i = 1; i <= d         


        
4条回答
  •  情深已故
    2021-02-19 06:12

    The problem stems from your DataGridViewColumn.CellTemplate not being set.

    For this scenario a DataGridViewTextBoxCell as the CellTemplate should suffice.

           var daysCount = DateTime.DaysInMonth(DateTime.Now.Year, 1);
    
            for (int i = 1; i <= daysCount; i++)
            {
                dataGridView1.Columns.Add(new DataGridViewColumn() { HeaderText = i.ToString(), CellTemplate = new DataGridViewTextBoxCell() });
            }
    

提交回复
热议问题