Dan McCoy

Dan McCoy

  • NA
  • 8
  • 0

Set value of DataGridViewTextBoxCell

Aug 9 2007 7:37 AM
I am struglling with an unbound DataGridView.
I would like to be able to programmatically add a new row and then be able to set the values of the cells. However, whe I use:

Cell.Value = "This is the text";

this throws an ArgumentOutOfRangeException.

Here is some example code (that doesn't work):


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DataGridTest
{
    public partial class Form1 : Form
    {

        private System.Windows.Forms.DataGridViewComboBoxColumn colCombo = new DataGridViewComboBoxColumn();
        private System.Windows.Forms.DataGridViewTextBoxColumn colText = new DataGridViewTextBoxColumn();
        private System.Windows.Forms.DataGridViewCheckBoxColumn colCheckBox = new DataGridViewCheckBoxColumn();
       
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns.AddRange( new DataGridViewColumn[] { colCombo, colText, colCheckBox });

            DataGridViewRow oRow = new DataGridViewRow();

            dataGridView1.Rows.Add(oRow);

            DataGridViewTextBoxCell oCell = (DataGridViewTextBoxCell)oRow.Cells[1];
            oCell.Value = "Some text";
        }
    }
}

How should I be going about setting the values of the cells?
I'd rather not have to bind this to a data table.