簡體   English   中英

DataGridview單元格在WinForms應用程序C#中不消耗

[英]DataGridview cell not expending in WinForms Application C#

我有一個簡單的datagridview包含兩列。 我想通過按按鈕到column1單元格從文本框中添加文本,但是它僅顯示一個文本,而不顯示其他文本,第一個文本替換第二個文本。

我想通過按按鈕將文本框中的文本添加到column1單元格中。

如圖所示

圖片

      this.dataGridView1.DefaultCellStyle.WrapMode =
         DataGridViewTriState.True;
        dataGridView1.AutoSizeRowsMode = 
     DataGridViewAutoSizeRowsMode.AllCells;

        dataGridView1.Rows[0].Cells[0].Value = textBox1.Text + 
      Environment.NewLine;

然后,您應該嘗試這樣的事情:

        string tmp = "";
    private void button1_Click(object sender, EventArgs e)
    {
        this.dataGridView1.DefaultCellStyle.WrapMode =
     DataGridViewTriState.True;
        dataGridView1.AutoSizeRowsMode =
     DataGridViewAutoSizeRowsMode.AllCells;

        tmp += textBox1.Text + Environment.NewLine;

        dataGridView1.Rows[0].Cells[0].Value = tmp;
    }

做些細微的改變將幫助您達到預期的效果

dataGridView1.Rows[0].Cells[0].Value += textBox1.Text + Environment.NewLine;

要么

讀取單元格數據並追加新數據,然后替換單元格值

        this.dataGridView1.DefaultCellStyle.WrapMode =
 DataGridViewTriState.True;
        dataGridView1.AutoSizeRowsMode =
     DataGridViewAutoSizeRowsMode.AllCells;
        var tmp = dataGridView1.Rows[0].Cells[0].Value;
        tmp += textBox1.Text + Environment.NewLine;

        dataGridView1.Rows[0].Cells[0].Value = tmp;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM