C# 如何在WindowForm c中设置DataGridViewTextBoxCell中的值#

C# 如何在WindowForm c中设置DataGridViewTextBoxCell中的值#,c#,datagridview,datagridviewtextboxcell,C#,Datagridview,Datagridviewtextboxcell,无法在DataGridViewTextBox中设置值 这是我的密码 DataGridViewTextBoxColumn tbCol = new DataGridViewTextBoxColumn(); DataGridViewTextBoxCell tbCell = new DataGridViewTextBoxCell(); tbCell.Value = "1"; tbCol.CellTemplate = tbCell; tbCol.Name = "qtySelect"; tbCol.Head

无法在DataGridViewTextBox中设置值

这是我的密码

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

gridProduct.Columns.Add(tbCol);
当我运行时,文本框被添加,但它是一个空白文本框

任何人都可以帮助我

谢谢。

您应该这样做:

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

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


文档中不清楚,但我认为CellTemplate只提供了单元格样式,而不是单元格值。
        // 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