6 Replies - 1614 Views - Last Post: 08 November 2018 - 06:48 PM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 185
  • Joined: 24-January 14

Select row from datagridview1 not display in datagridview2 and show wr

Posted 09 May 2018 - 04:41 PM

Select row from datagridview1 not display in datagridview2 and show wrong value
Problem
IF I have two rows in datagridview1 and select second one it display as first meaning
if i have in datagridview1 two records
22
24
and select 22 first it move to datagridview2 success but 24 when select it from datagridview1 it display as 22 in datagridview2
why this idont know
I have two datagridviews
datagridview1 represent getselectrow
datagridview2 represent tbRes
my code as below in cell click event of first datagridview1
IndexValSold = Grid.CurrentRow.Index;
            getselectrow = GetSelectedRowsVal();
            foreach (DataGridViewRow row in datagridview1.SelectedRows)
            {
                
                tbRes.ImportRow(getselectrow.Rows[row.Index]);
               datagridview1.Rows.RemoveAt(row.Index);
                //getselectrow.Rows.RemoveAt(row.Index);

            }
            tbRes.AcceptChanges();

            datagridview12.DataSource = tbRes;
            getselectrow.Rows.RemoveAt(IndexValSold);

http://www.mediafire...3hqfyqkc/24.jpg

Is This A Good Question/Topic? 0
  • +

Replies To: Select row from datagridview1 not display in datagridview2 and show wr

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6807
  • View blog
  • Posts: 28,141
  • Joined: 12-December 12

Re: Select row from datagridview1 not display in datagridview2 and show wr

Posted 10 May 2018 - 01:01 AM

Question moved out of C# Discussion Lounge. You are seeking help with code not introducing a discussion topic.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6988
  • View blog
  • Posts: 23,763
  • Joined: 05-May 12

Re: Select row from datagridview1 not display in datagridview2 and show wr

Posted 10 May 2018 - 10:47 AM

WinForms is long at end of life. I've not deal with it deeply in years, but I vaguely recall something about changing the DataSource of a datagrid view needs some binding steps need to be done if it is already showing some data.

I highly suggest abandoning this code and using WPF or Xamarin which does data binding the right way rather than the partially implemented way it is done in WinForms.
Was This Post Helpful? 0
  • +
  • -

#4 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Select row from datagridview1 not display in datagridview2 and show wr

Posted 06 November 2018 - 02:17 PM

View Postahmedba, on 09 May 2018 - 04:41 PM, said:

Select row from datagridview1 not display in datagridview2 and show wrong value
Problem

datagridview2 represent tbRes
my code as below in cell click event of first datagridview1
IndexValSold = Grid.CurrentRow.Index;
            getselectrow = GetSelectedRowsVal();
            foreach (DataGridViewRow row in datagridview1.SelectedRows)
            {
                
                tbRes.ImportRow(getselectrow.Rows[row.Index]);
               datagridview1.Rows.RemoveAt(row.Index);
                //getselectrow.Rows.RemoveAt(row.Index);

            }
            tbRes.AcceptChanges();

            datagridview12.DataSource = tbRes;
            getselectrow.Rows.RemoveAt(IndexValSold);

http://www.mediafire...3hqfyqkc/24.jpg


try adding
datagridview12.Clear(); 

before you re-assign the DataSource .. I've noticed problems if the DataSource is already assigned and I try to assign another. It doesn't display properly

here's how I handle something similar but not the exact same; you will need to edit this code but it might help.

            ListViewItem lvi = new ListViewItem();



            string line1 = File.ReadLines("C:\\AD_Project\\testPrinter_.csv").First();

            if (line1.Substring(0,1) == "#")
            {

                //readlines
                    var lines = System.IO.File.ReadAllLines("C:\\AD_project\\testPrinter_.csv");

                //skip the first line and then rewrite the file 
                   System.IO.File.WriteAllLines("C:\\AD_project\\testPrinter_.csv", lines.Skip(1).ToArray());
            }

            if (delimitersTextBox.Text == "")
            {
                MessageBox.Show("Please Enter a Delimiter", "Message");
                return;
            }

            // Open a TextFieldParser using these delimiters.
            string[] delimiters = {delimitersTextBox.Text};
            
            using (TextFieldParser parser =
                FileSystem.OpenTextFieldParser("C:\\AD_project\\testPrinter_.csv", delimiters))
            {
                // Process the file's lines.
                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();



                    foreach (string t in fields)
                    {
                        try
                        {
                            dt.Columns.Add(t, typeof(string));
                            listBoxColumns.Items.Add(t);
                        }
                        catch
                        {
                            dt.Clear();
                        }
                    }
                    // lvi.SubItems.Add(pet.Type);

                    for (int i = 1; i < fields.Length; i++)
                    {

                        string[] t = parser.ReadFields();
                        if (t != null)
                        {
                            textBox3.Text = t.ToString();

                            dt.Rows.Add(t);
                        }
                    }

                    dataGridView1.DataSource = dt;

                    dataGridView2.DataSource = dt;
                    


Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Select row from datagridview1 not display in datagridview2 and show wr

Posted 07 November 2018 - 06:26 AM

Ehh the topic is six months old hexagod.

I doubt the OP is still interested in this post at this point, and I'm sure you can find more up-to-date topics to help-out on.
Was This Post Helpful? 0
  • +
  • -

#6 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: Select row from datagridview1 not display in datagridview2 and show wr

Posted 08 November 2018 - 09:32 AM

yeah I didn't notice that, whoopsies. I need to quit with the necroposting. I'll work on paying more attention to the dates.

To make matters worse, his english is not the greatest so I doubt this would be of any use to future devs. My bad

This post has been edited by hexagod: 08 November 2018 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Select row from datagridview1 not display in datagridview2 and show wr

Posted 08 November 2018 - 06:48 PM

Yea its something to pay attention to, but hay, you're not alone, I've done it myself many a time lol. Sometimes we just don't think to check the dates when posting. It happens 🙂
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1