1

I can't set Value in DataGridViewTextBox

Here is my code

DataGridViewTextBoxColumn tbCol = new DataGridViewTextBoxColumn();
DataGridViewTextBoxCell tbCell = new DataGridViewTextBoxCell();
tbCell.Value = "1";
tbCol.CellTemplate = tbCell;
tbCol.Name = "qtySelect";
tbCol.HeaderText = "เลือกจำนวน"; 

gridProduct.Columns.Add(tbCol);

When I Run Textbox are added but it's a blank textbox

any one can help me.

Thank.

Chanom First
  • 1,136
  • 1
  • 11
  • 25
  • It's not clear in the documentation, but I think the CellTemplate just provides the cell style, not the cell value. – user2867342 Feb 17 '15 at 16:48

2 Answers2

1

You should do this:

yourdatagridview["columnName", rowindex].Value = "Your value";
Jéf Bueno
  • 425
  • 1
  • 5
  • 23
1

DataGridViewTextBoxColumn tbCol = new DataGridViewTextBoxColumn(); DataGridViewTextBoxCell tbCell = new DataGridViewTextBoxCell();

        // Used for the appearence of the cell, it doesn't mean that there is a correlation between the cell and the column (see below ***)
        tbCell.Style.ForeColor = Color.Blue;
        tbCol.CellTemplate = tbCell;

        tbCol.Name = "qtySelect";
        tbCol.HeaderText = "Header";

        int colIndex = gridProduct.Columns.Add(tbCol);
        const int rowNumber = 0;    // define which row you want

        //Pay attention that you may need to add rows before writting to the cell
        //const int rowNumber = 1;
        //gridProduct.Rows.Add();            

        gridProduct.Rows[rowNumber].Cells[colIndex].Value = "Value for cell";

        //*** If you want to work with the cell itself, you first need to get a reference to it
        //tbCell = gridProduct.Rows[0].Cells[0] as DataGridViewTextBoxCell;
        //tbCell.Value = "1"; // and then you can overwrite the value
ehh
  • 3,412
  • 7
  • 43
  • 91