简体   繁体   English

DataGridview单元格在WinForms应用程序C#中不消耗

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

I have a simple datagridview contain two columns . 我有一个简单的datagridview包含两列。 I want to add text from textbox by pressing a button to column1 cell but it show only one text not the other and first one text replace the second one . 我想通过按按钮到column1单元格从文本框中添加文本,但是它仅显示一个文本,而不显示其他文本,第一个文本替换第二个文本。

I want to add text from textbox to column1 cell as much as I want by pressing a button . 我想通过按按钮将文本框中的文本添加到column1单元格中。

as in image showed 如图所示

图片

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

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

Then you should try something like this: 然后,您应该尝试这样的事情:

        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;
    }

doing slight change will help you to achieve the expected result 做些细微的改变将帮助您达到预期的效果

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

or 要么

Read your cell data and append your new data and then replace the cell value 读取单元格数据并追加新数据,然后替换单元格值

        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