Rows Cannot be Programmatically added.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • M1kkelZU
    New Member
    • Feb 2013
    • 80

    Rows Cannot be Programmatically added.

    Hey guys, I'm getting an error when I want t add a file to my dataGridView that I can't add any data because it is databound. I've been working around this but still gives me the same error. heres the snippet of the code:
    Code:
           private void btnOpenLog_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    String sLine = "";
                    try
                    {
                        System.IO.StreamReader FileStream = new System.IO.StreamReader(openFileDialog1.FileName);
                        sLine = FileStream.ReadLine();
                        string[] s = sLine.Split(';');
                        for (int i = 0; i <= s.Count() - 1; i++)
                        {
                            DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
                            colHold.Name = "col" + System.Convert.ToString(i);
                            colHold.HeaderText = s[i].ToString();
                            dataGridView1.Columns.Add(colHold);
                        }
                        sLine = FileStream.ReadLine();
                        while (sLine != null)
                        {
                            dataGridView1.Rows.Add();
                            for (int i = 0; i <= s.Count() - 1; i++)
                            {
                                s = sLine.Split('|');
                                dataGridView1.Rows[dTable.Rows.Count - 1].Cells[i].Value = s[i].ToString();
                            }
                            sLine = FileStream.ReadLine();
                        }
                        FileStream.Close();
                    }
                    catch (Exception err)
                    {
                        System.Windows.Forms.MessageBox.Show("Error:  " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
    If anyone can help that would be awesome.

    Thanks.
  • Mikkeee
    New Member
    • Feb 2013
    • 94

    #2
    I would instead create and populate a datatable and then set the datasource of your GridView to the datatable.

    Comment

    • M1kkelZU
      New Member
      • Feb 2013
      • 80

      #3
      Hmm. I'll have a look at that. Where would I place the right statements etc then?

      Comment

      • Mikkeee
        New Member
        • Feb 2013
        • 94

        #4
        Your code is almost there right now. First create a new datatable and add the columns inside your first loop where you're populating your gridview columns.
        Code:
        dt.Columns.Add(String.Format("col{0}",i), typeof(string));
        And then in your code where you're reading your file you need to add the data to the data table.
        Code:
        row[i] = i.ToString();
        Place the row into the datatable after you're done filling the row with values.
        Code:
        dt.Rows.Add(row);
        And you need to set the datasource when you're done.
        Code:
        dataGridView1.DataSource = dt;

        Comment

        • M1kkelZU
          New Member
          • Feb 2013
          • 80

          #5
          Ok thanks, only thing is I can't use the
          Code:
          row = i.ToString();
          as it gives me an error that I don't have row declared and can't use
          Code:
          DataRow row = new DataRow();
          as it starts whining that it can't convert DataRow to String.

          Comment

          • Mikkeee
            New Member
            • Feb 2013
            • 94

            #6
            You do need to declare your DataRow first but you missed the [i] part after the row;

            Code:
            row[B][i][/B] = i.ToString();

            Comment

            • M1kkelZU
              New Member
              • Feb 2013
              • 80

              #7
              oh lol I'm dumb. Now I at least don't get an errr that It can't add the data. Now it gives me a nicer error:
              Code:
              Input array is longer than the numer of columns in this table.
              I have added a new column and everthing but it still doesnt work :/

              Comment

              • Mikkeee
                New Member
                • Feb 2013
                • 94

                #8
                Please re-post your modified code.

                Comment

                • M1kkelZU
                  New Member
                  • Feb 2013
                  • 80

                  #9
                  Code:
                  private void btnOpenLog_Click(object sender, EventArgs e)
                          {
                              OpenFileDialog openFileDialog1 = new OpenFileDialog();
                              if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
                              {
                                  String sLine = "";
                                  try
                                  {
                                      StreamReader FileStream = new StreamReader(openFileDialog1.FileName);
                                      sLine = FileStream.ReadLine();
                                      string[] s = sLine.Split(';');
                                      for (int i = 0; i <= s.Count() - 1; i++)
                                      {
                                          dTable.Columns.Add(String.Format("col{0}",i), typeof(string));
                                      }
                                      sLine = FileStream.ReadLine();
                                      while (sLine != null)
                                      {
                                          //dataGridView1.Rows.Add();
                                          for (int i = 0; i <= s.Count() - 1; i++)
                                          {
                                              s = sLine.Split('|');
                                              DataRow row = dTable.NewRow();
                                              row[i] = i.ToString();
                                              dTable.Rows.Add(s);
                                          }
                                          sLine = FileStream.ReadLine();
                                      }
                                      dataGridView1.DataSource = dTable;
                                      FileStream.Close();
                                  }
                                  catch (Exception err)
                                  {
                                      System.Windows.Forms.MessageBox.Show("Error:  " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                  }
                              }
                          }
                  Here you go, if needed I'll upload the entire project for you.

                  Comment

                  • Mikkeee
                    New Member
                    • Feb 2013
                    • 94

                    #10
                    You have a few lines of code inside a for loop that should be outside the for loop.
                    Change:
                    Code:
                    for (int i = 0; i <= s.Count() - 1; i++)
                    {
                        s = sLine.Split('|');
                        DataRow row = dTable.NewRow();
                        row[i] = i.ToString();
                        dTable.Rows.Add(s);
                    }
                    To:
                    Code:
                    s = sLine.Split('|');
                    DataRow row = dTable.NewRow();
                    for (int i = 0; i <= s.Count() - 1; i++)
                    {
                        row[i] = s[i].ToString();
                    }
                    // You need to add the row, not the 's' array
                    dTable.Rows.Add(row);

                    Comment

                    • M1kkelZU
                      New Member
                      • Feb 2013
                      • 80

                      #11
                      ok thanks, now the only thing its saying is
                      Code:
                      Can't find column 1
                      Any idea what could be going on?

                      Comment

                      • Mikkeee
                        New Member
                        • Feb 2013
                        • 94

                        #12
                        Which line of code is giving you that error?

                        Comment

                        • M1kkelZU
                          New Member
                          • Feb 2013
                          • 80

                          #13
                          I have no clue, Its the Exception MessageBox that says that.

                          Comment

                          • Mikkeee
                            New Member
                            • Feb 2013
                            • 94

                            #14
                            Well.. you need to be sure that the number of columns defined match the number values coming in. It does appear strange to me that you're pulling the first record of your file to create the column names which is defined by a semi-colon but you're delimiting the actual data with the pipe. I wish I could give you an answer here but really need to step into your code to find out which line is causing the issue. A very basic first step would be to learn how to debug your code. This will help you tremendously and save you tons of time in the future once you understand how to properly debug and use try/catch. Just search google for 'c sharp debugging tutorial' and you will find a bunch of tutorials.

                            Comment

                            • M1kkelZU
                              New Member
                              • Feb 2013
                              • 80

                              #15
                              Ah ok thanks :) Atleast its working that 1% extra.

                              Comment

                              Working...