0

Here is the question. I have a restaurant program in which customer chooses what he wishes to eat and that data is displayed in dataGridView. For example 6 different dishes = 6 different rows each containing Name, Amount and Price. After all I need to print a bill, so I want to get all information from dataGridView to a textbox. All rows. How I can do that?

P.S. I've searched a lot but there is only information on how to transfer CurrentRow data to a textbox that I've found.

 private void Form4_Load(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        string billinfo = string.Empty;
        foreach (DataGridViewRow row in f1.dataGridView1.Rows)
        {
            billinfo = string.Format("{0}{1} {2} {3}{4}", billinfo, row.Cells["Name"].Value, row.Cells["Amount"].Value, row.Cells["Price"].Value, Environment.NewLine);
        }
        textBox1.Text = billinfo;
    }
Martin
  • 177
  • 1
  • 11

2 Answers2

0

Just loop through each row and format the column values into a string, adding System.Environment.NewLine for seperating entries in the TextBox.

string billInfo = string.Empty;

foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
  billInfo = string.Format("{0}{1} {2} {3}{4}", billInfo, row.Cells["Name"].Value, row.Cells["Amount"].Value, row.Cells["Price"].Value, Environment.NewLine);
}

this.textBox1.Text = billInfo;
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • I tried your method. But Im trying to get dataGridView values from another form and it doesn't work, my code looks like that. See my edited post. – Martin Apr 14 '15 at 20:56
  • @user1898760 Is the `DataGridView` in `Form1` public? When you added the columns did you name them *Name*, *Amount*, and *Price*? If yes to both, what error / result are you seeing? – OhBeWise Apr 14 '15 at 21:02
  • yes for both questions. I even tried to write 0,1,2 instead of column names. I don't get any kind of errors, I just get empty textbox without any information. – Martin Apr 14 '15 at 21:16
  • @user1898760 This leads me to believe when you declare a new instance of `Form1`, its constructor must take care of adding the correct columns to the `DataGridView` but is not responsible for populating the rows (hence, no errors and no data). If `Form1` already exists and through some chain of hierarchy parents `Form4`, then `Form4` shouldn't declare a new instance of `Form1` but should instead reference the original. This is one possibility of what could be happening. – OhBeWise Apr 14 '15 at 21:29
  • The thing is that it's not parented in any way instead of declaring new instance. By the way, I tried to get data from textbox on `Form1` to a textbox on `Form4` and it worked perfectly. I don't really know what to do now to get data from dataGridView... – Martin Apr 14 '15 at 21:43
  • Well, it looks like i can't do it even with textbox now. – Martin Apr 14 '15 at 22:04
  • I found the way to do what I wanted. Great thanks for your help! – Martin Apr 14 '15 at 22:23
0

It looks you have multiple forms. Each form will have its own "Context" to keep track of what "controls" belong to each form.

In your code"Form4_Load" is an event triggered during the launch of "Form4". Then in this method you create "new Form1()". This is new empty Form of type Form1. But it is not related or linked in any way to you first instance of "Form1". You have to use the already created "f1".

A crude way to fix your program would be like this (below) but it is not a good practice to grab global instances of objects and use. Its very easy to loose track of what objects are valid. Anyway here is my simple fix.

enter code here


private void Form4_Load(object sender, EventArgs e)
    {
        //I am assuming f1 is the name of your original Form1
        String f1DataGridViewName = f1.dataGridView1.Name.ToString();
        int f1RowCount = f1.RowCount;
        string billinfo = string.Empty;
        Console.Writeline("Form1 f1 Datagridview name is: {0}
                           ,Form1 f1 DataGridview row count is : {1}"
                           ,f1DataGridViewName, f1RowCount );
        foreach (DataGridViewRow row in f1.dataGridView1.Rows)
        {
            billinfo = string.Format("{0}{1} {2} {3}{4}", billinfo
                     , row.Cells["Name"].Value
                     , row.Cells["Amount"].Value
                     , row.Cells["Price"].Value
                     , Environment.NewLine);
        }
        textBox1.Text = billinfo;
    }