Hey... I have dataGridView and button „Edit“ for editing informations of the dataGridView and now I'm trying to read the informations in the firts form, and write them in to the second form but when I clicked on the „Edit“ button I get error message „Input string was not in a correct format“ and the informations in the second form are given in a random order (e.g First name: 223548745, Last Name: New York, Home Number: John) :/

Here is my code:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string queryString = "SELECT FirstName, LastName, HomeNumber, MobileNumber, eMail, WebSite FROM Friends";

            int currentRow = int.Parse(e.RowIndex.ToString());
            
            try
            {
                /* I mean this is incorrect */
                string friendsIDString = dataGridView1[0, currentRow].Value.ToString();
                friendsIDInt = int.Parse(friendsIDString);
            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

            // Funkcija za dugmeto "Promeni"
            if (dataGridView1.Columns[e.ColumnIndex] == editButton && currentRow >= 0)
            {
                string firstName = dataGridView1[1, currentRow].Value.ToString();
                string lastName = dataGridView1[2, currentRow].Value.ToString();
                string homeNumber = dataGridView1[3, currentRow].Value.ToString();
                string mobileNumber = dataGridView1[4, currentRow].Value.ToString();
                string eMail = dataGridView1[5, currentRow].Value.ToString();
                string webPage = dataGridView1[6, currentRow].Value.ToString();

                // Start Form for Editing
                EditFriends frmEditFriends = new EditFriends();
                frmEditFriends.firstName2 = firstName;
                frmEditFriends.lastName2 = lastName;
                frmEditFriends.homeNumber2 = homeNumber;
                frmEditFriends.mobileNumber2 = mobileNumber;
                frmEditFriends.eMail2 = eMail;
                frmEditFriends.webPage2 = webPage;
                frmEditFriends.friendsID2 = friendsIDInt;
                frmEditFriends.Show();
                dataGridView1.Update();
            }
....
....

Thanks...

Recommended Answers

All 9 Replies

//int currentRow = int.Parse(e.RowIndex.ToString());
      //Change to
      int currentRow = e.RowIndex;

For your exception it looks like you're trying to convert a non-numeric string to an integer. Please examine your data. This should help:

int friendsIDInt;
      string friendsIDString = dataGridView1[0, currentRow].Value.ToString();
      if (!(int.TryParse(friendsIDString, out friendsIDInt));
      {
        MessageBox.Show("The following value is not an integer and could not be parsed : " +friendsIDString);
        System.Diagnostics.Debugger.Break();
      }
//int currentRow = int.Parse(e.RowIndex.ToString());
      //Change to
      int currentRow = e.RowIndex;

For your exception it looks like you're trying to convert a non-numeric string to an integer. Please examine your data. This should help:

int friendsIDInt;
      string friendsIDString = dataGridView1[0, currentRow].Value.ToString();
      if (!(int.TryParse(friendsIDString, out friendsIDInt));
      {
        MessageBox.Show("The following value is not an integer and could not be parsed : " +friendsIDString);
        System.Diagnostics.Debugger.Break();
      }

Thanks for your help... but look at the picture... I don`t understand, all the information are with mixed order :/

My dataGridView1 is:

public void loadDataGrid(string sqlQueryString)
        {

            OleDbCommand SQLQuery = new OleDbCommand();
            DataTable data = null;
            dataGridView1.DataSource = null;
            SQLQuery.Connection = null;
            OleDbDataAdapter dataAdapter = null;
            dataGridView1.Columns.Clear(); // <-- clear columns
            //---------------------------------
            SQLQuery.CommandText = sqlQueryString;
            SQLQuery.Connection = database;
            data = new DataTable();
            dataAdapter = new OleDbDataAdapter(SQLQuery);
            dataAdapter.Fill(data);
            dataGridView1.DataSource = data;
            dataGridView1.AllowUserToAddRows = false; // remove the null line
            dataGridView1.ReadOnly = true;
            dataGridView1.Columns[0].Visible = true;

            // DataGridView Kolona 1...
            dataGridView1.Columns[1].Width = 78;
            dataGridView1.Columns[1].HeaderText = "Име";

            // DataGridView Kolona 1...Име на Издавач ###
            dataGridView1.Columns[2].Width = 120;
            dataGridView1.Columns[2].HeaderText = "Презиме / Ник";

            // DataGridView Kolona 3...
            dataGridView1.Columns[3].Width = 100;
            dataGridView1.Columns[3].HeaderText = "Домашен";

            // DataGridView Kolona 4...
            dataGridView1.Columns[4].Width = 100;
            dataGridView1.Columns[4].HeaderText = "Мобилен";

            // DataGridView Kolona 5...
            dataGridView1.Columns[5].Width = 130;
            dataGridView1.Columns[5].HeaderText = "Меил";

            // DataGridView Kolona 5...
            dataGridView1.Columns[6].Width = 140;
            dataGridView1.Columns[6].HeaderText = "Веб Сајт";

            // insert edit button into datagridview
            editButton = new DataGridViewButtonColumn();
            editButton.HeaderText = "Промени";
            editButton.Text = "Промени";
            editButton.UseColumnTextForButtonValue = true;
            editButton.Width = 75;
            editButton.ToolTipText = "Променете ги податоците за членот...";
            dataGridView1.Columns.Add(editButton);

            // insert delete button to datagridview
            deleteButton = new DataGridViewButtonColumn();
            deleteButton.HeaderText = "Бриши";
            deleteButton.Text = "Бриши";
            deleteButton.UseColumnTextForButtonValue = true;
            deleteButton.Width = 75;
            deleteButton.ToolTipText = "Избришете го членот од листата...";
            dataGridView1.Columns.Add(deleteButton);
        }

And now...

// This should be a Name but this is "remove" button
                dataGridView1[1, currentRow].Value.ToString();

                // This should be a Last Name but this is ID
                dataGridView1[2, currentRow].Value.ToString();

                // This should be a Home Number but this is Name
                dataGridView1[3, currentRow].Value.ToString();

                // This should be a Mobile Number but this is Last Name
                dataGridView1[4, currentRow].Value.ToString();

                // This should be a Meil but this is Home Number
                dataGridView1[5, currentRow].Value.ToString();

                // This should be a Web Site but this is Mobile Number
                dataGridView1[6, currentRow].Value.ToString();

I can't understand how can this... Thanks.

Maybe your select query doesn't line up with the columns? Do you have the field named set in the column options? I don't know, its hard to say. I can walk you through how you can save the data if you're willing to upload the project so I can take a look at it.

Thanks man for you help.
I tryed a couple of things to solve the problem, but none of them woked... so, if you want you can take a look at my project/code...

- The Project
- Only Form1.cs

Thanks so much. ;-)

Eh actually this won't work. I can't read your native language plus I can't populate the data since I don't have access to your datasource.

If you can create a test project without relying on an external datasource and use english characters I can try to help you. Sorry for all the legwork :(

Ohh I forgot for the language and characters... sorry :(

Here is english version:
- The Project
- Only Form1.cs

The database is MS Access database and without password...

P.S: Thanks so much, and don't worry for the legwork :)

I don't know how to explain it but the gridView is reArranging the columns.

In the loadDataGrid() method name the friendId column:

dataGridView1.Columns[0].Visible = false;
          //sk - added  
          dataGridView1.Columns[0].Name = "friendID";

Then in your dataGridView1_CellContentClick() change this line:

string friendsIDString = dataGridView1[dataGridView1.Columns["friendId"].Index, currentRow].Value.ToString();

When your grid is loading column 0 is the friend ID. When the CellContentClick column[0] is the edit button, and column[1] is the delete button. Weird. You should always rely on names :)

commented: Thanks man... +1

Ou man, it works... I love you :)

Thanks so much and sorry for the legwork. :(

commented: welcome to daniweb! I look forward to seeing your posts +7

You're welcome.

Please mark this thread as solved if you have found an answer to your problem and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.