Tuesday, 9 October 2012

Datagridview create rows progrmatically, readonly rows, read only cells

How to create rows progrmatically in the datagridview?

How to make certain rows as read-only?

How to make certain cells as read-only?

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int numberOfColumns = 10;
        const int numberOfRows = 5;
        public Form1()
        {
            InitializeComponent();
            for (int columnCount = 0; columnCount < numberOfColumns; 
                   columnCount++)
            {
                DataGridViewColumn NewColumn =
                   new DataGridViewColumn();
                DataGridViewCell cell = new DataGridViewTextBoxCell();
                cell.Style.BackColor = Color.Wheat;
                NewColumn.CellTemplate = cell;
                dataGridView1.Columns.Add(NewColumn);
            }
            int RowNumber;
            DataGridViewRow NewRow;
            for(int rowCount = 0; rowCount < numberOfRows; rowCount++)
            {
               RowNumber = dataGridView1.Rows.Add();
               NewRow = dataGridView1.Rows[RowNumber];
               for (int columnCount = 0; columnCount < numberOfColumns; 
                  columnCount++)
               {
                   NewRow.Cells[columnCount].Value = columnCount.ToString();
               }
               NewRow.ReadOnly = true;
            }
 
        }
    }
}

 

No comments:

Post a Comment