9 Replies - 1430 Views - Last Post: 31 October 2013 - 08:49 AM Rate Topic: -----

#1 Messerschmitt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-October 13

Error getting a string value from DataGridView

Posted 31 October 2013 - 07:58 AM

Hi there. I'm quiet new to C#, I'm trying to write a history page which pops up as a separate form and shows the urls the user has visited. All the urls are saved in a list of strings, then transform to DataTable and put the values into DataGridView. I can see the table with urls. I wanted to enable doubleclicking on the cell, so that if user doubleclicks on it, he will be transformed to that url.

I got an error when selecting a row and and parsing it to string.

        public void pageload(List<string> list)
        {
            table = ConvertListToDataTable(list);
            dataGridView1.DataSource = table;
        }

        private DataTable ConvertListToDataTable(List<string> l)
        {
            table.Columns.Add("urls");
            int i = 0;
            foreach(string s in l)
            {
                table.Rows.Add();
                table.Rows[i].SetField("urls", s);
                i++;
            }
            return table;
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Form1 form = new Form1();
            form.pageInput.Text = dataGridView1.SelectedRows[0].ToString();//error in here
            string input = form.pageInput.Text;
            form.loadPage(input);
        }




Does this mean I have to get the whole datatable first, then converting it back to list and search for the value that user double clicked and then pass the parameters to the main form to load the url? Any suggestions? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Error getting a string value from DataGridView

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:03 AM

Can't help you if you don't tell us what the error actually is.
Was This Post Helpful? 0
  • +
  • -

#3 Messerschmitt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-October 13

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:17 AM

View PostCurtis Rutland, on 31 October 2013 - 08:03 AM, said:

Can't help you if you don't tell us what the error actually is.


Hi there. I probably should've made it clear. As I've written down that the problem is getting a string value from the selected DataGridView cell. I got an error when getting a the value of a cell that user double clicked and converting it to a string. If you look at the code I've put comments on the line I got an error, it's when I do this:

form.pageInput.Text = dataGridView1.SelectedRows[0].ToString();

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:22 AM

Yes, I saw the comment, but you've still not made it clear. Copy and paste your exact error message. "I got an error" isn't helpful; the actual type and text of an exception are helpful.
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:31 AM

My guess is a NullReferenceException. What if you don't have any SeelctedRows when you double-click a cell? There won't be anything in this collection unless you have selected an entire row. Besides that, I don't think you want to convert an entire row of displayed data to a string and display it. Do you just want the value in the cell that's been double-clicked? In which case, you'll probably find that "sender" is the DataGridViewCell that was clicked. Otherwise, the "e" variable will include Column and Row indices.
Was This Post Helpful? 0
  • +
  • -

#6 Messerschmitt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-October 13

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:34 AM

View PostCurtis Rutland, on 31 October 2013 - 08:22 AM, said:

Yes, I saw the comment, but you've still not made it clear. Copy and paste your exact error message. "I got an error" isn't helpful; the actual type and text of an exception are helpful.


It's ArgumentOutPGRangeException: Index was out of range. Must be non-negative and less than the size of collection. Parameter name:index.
Was This Post Helpful? 0
  • +
  • -

#7 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:35 AM

OK, ArgumentOutOfRange is a symptom of what I was talking about; I just guessed at the wrong exception.

This post has been edited by MrShoes: 31 October 2013 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 Messerschmitt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-October 13

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:39 AM

View PostMrShoes, on 31 October 2013 - 08:31 AM, said:

My guess is a NullReferenceException. What if you don't have any SeelctedRows when you double-click a cell? There won't be anything in this collection unless you have selected an entire row. Besides that, I don't think you want to convert an entire row of displayed data to a string and display it. Do you just want the value in the cell that's been double-clicked? In which case, you'll probably find that "sender" is the DataGridViewCell that was clicked. Otherwise, the "e" variable will include Column and Row indices.

That was ArgumentOutOfRangeException. The table just represents a list of urls, so it's one row per url.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:48 AM

Well, I'd suggest you put a breakpoint on that line that's throwing the error, and inspect the values. Index out of range errors are typically thrown from objects that use indexers. In this case, it looks like SelectedRows is what's throwing the error, since it's the only one with an obvious indexer (square brackets).

The debugger will let you inspect all values in scope, so you can really get a clear picture of what's going on in your application behind the scenes. It's easily the most important tool in a programmer's toolkit, and you should become intimately familiar with it.
Was This Post Helpful? 0
  • +
  • -

#10 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Error getting a string value from DataGridView

Posted 31 October 2013 - 08:49 AM

Yes. But look at what you're doing here: dataGridView1.SelectedRows[0].ToString(); You're trying to access the DataGridViewRow at index 0 in the collection dataGridView1.SelectedRows. Your error occurs because there are no items in that collection, therefore no row at position 0.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1