2015-02-17 2 views
0

я не могу установить значение в DataGridViewTextBoxКак установить значение в DataGridViewTextBoxCell в WindowForm C#

Вот мой код

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

gridProduct.Columns.Add(tbCol); 

Когда я запускаю Textbox добавлены, но это пустое текстовое поле

любой может мне помочь.

Thank.

+0

Это не ясно в документации, но я думаю, что CellTemplate просто предоставляет стиль ячейки, а не значение ячейки. – user2867342

ответ

0

Вы должны сделать это:

yourdatagridview["columnName", rowindex].Value = "Your value"; 
0

DataGridViewTextBoxColumn tbCol = новый DataGridViewTextBoxColumn(); DataGridViewTextBoxCell tbCell = новый 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 
Смежные вопросы