473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rows Cannot be Programmatically added.

80 64KB
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:
Expand|Select|Wrap|Line Numbers
  1.        private void btnOpenLog_Click(object sender, EventArgs e)
  2.         {
  3.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  4.             if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
  5.             {
  6.                 String sLine = "";
  7.                 try
  8.                 {
  9.                     System.IO.StreamReader FileStream = new System.IO.StreamReader(openFileDialog1.FileName);
  10.                     sLine = FileStream.ReadLine();
  11.                     string[] s = sLine.Split(';');
  12.                     for (int i = 0; i <= s.Count() - 1; i++)
  13.                     {
  14.                         DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
  15.                         colHold.Name = "col" + System.Convert.ToString(i);
  16.                         colHold.HeaderText = s[i].ToString();
  17.                         dataGridView1.Columns.Add(colHold);
  18.                     }
  19.                     sLine = FileStream.ReadLine();
  20.                     while (sLine != null)
  21.                     {
  22.                         dataGridView1.Rows.Add();
  23.                         for (int i = 0; i <= s.Count() - 1; i++)
  24.                         {
  25.                             s = sLine.Split('|');
  26.                             dataGridView1.Rows[dTable.Rows.Count - 1].Cells[i].Value = s[i].ToString();
  27.                         }
  28.                         sLine = FileStream.ReadLine();
  29.                     }
  30.                     FileStream.Close();
  31.                 }
  32.                 catch (Exception err)
  33.                 {
  34.                     System.Windows.Forms.MessageBox.Show("Error:  " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  35.                 }
  36.             }
  37.         }
If anyone can help that would be awesome.

Thanks.
Feb 15 '13 #1
18 4548
Mikkeee
94 64KB
I would instead create and populate a datatable and then set the datasource of your GridView to the datatable.
Feb 15 '13 #2
M1kkelZU
80 64KB
Hmm. I'll have a look at that. Where would I place the right statements etc then?
Feb 15 '13 #3
Mikkeee
94 64KB
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.
Expand|Select|Wrap|Line Numbers
  1. dt.Columns.Add(String.Format("col{0}",i), typeof(string));
  2.  
And then in your code where you're reading your file you need to add the data to the data table.
Expand|Select|Wrap|Line Numbers
  1. row[i] = i.ToString();
  2.  
Place the row into the datatable after you're done filling the row with values.
Expand|Select|Wrap|Line Numbers
  1. dt.Rows.Add(row);
  2.  
And you need to set the datasource when you're done.
Expand|Select|Wrap|Line Numbers
  1. dataGridView1.DataSource = dt;
  2.  
Feb 15 '13 #4
M1kkelZU
80 64KB
Ok thanks, only thing is I can't use the
Expand|Select|Wrap|Line Numbers
  1. row = i.ToString();
as it gives me an error that I don't have row declared and can't use
Expand|Select|Wrap|Line Numbers
  1. DataRow row = new DataRow();
as it starts whining that it can't convert DataRow to String.
Feb 15 '13 #5
Mikkeee
94 64KB
You do need to declare your DataRow first but you missed the [i] part after the row;

Expand|Select|Wrap|Line Numbers
  1. row[i] = i.ToString();
Feb 15 '13 #6
M1kkelZU
80 64KB
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:
Expand|Select|Wrap|Line Numbers
  1. 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 :/
Feb 15 '13 #7
Mikkeee
94 64KB
Please re-post your modified code.
Feb 15 '13 #8
M1kkelZU
80 64KB
Expand|Select|Wrap|Line Numbers
  1. private void btnOpenLog_Click(object sender, EventArgs e)
  2.         {
  3.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  4.             if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
  5.             {
  6.                 String sLine = "";
  7.                 try
  8.                 {
  9.                     StreamReader FileStream = new StreamReader(openFileDialog1.FileName);
  10.                     sLine = FileStream.ReadLine();
  11.                     string[] s = sLine.Split(';');
  12.                     for (int i = 0; i <= s.Count() - 1; i++)
  13.                     {
  14.                         dTable.Columns.Add(String.Format("col{0}",i), typeof(string));
  15.                     }
  16.                     sLine = FileStream.ReadLine();
  17.                     while (sLine != null)
  18.                     {
  19.                         //dataGridView1.Rows.Add();
  20.                         for (int i = 0; i <= s.Count() - 1; i++)
  21.                         {
  22.                             s = sLine.Split('|');
  23.                             DataRow row = dTable.NewRow();
  24.                             row[i] = i.ToString();
  25.                             dTable.Rows.Add(s);
  26.                         }
  27.                         sLine = FileStream.ReadLine();
  28.                     }
  29.                     dataGridView1.DataSource = dTable;
  30.                     FileStream.Close();
  31.                 }
  32.                 catch (Exception err)
  33.                 {
  34.                     System.Windows.Forms.MessageBox.Show("Error:  " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  35.                 }
  36.             }
  37.         }
Here you go, if needed I'll upload the entire project for you.
Feb 15 '13 #9
Mikkeee
94 64KB
You have a few lines of code inside a for loop that should be outside the for loop.
Change:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i <= s.Count() - 1; i++)
  2. {
  3.     s = sLine.Split('|');
  4.     DataRow row = dTable.NewRow();
  5.     row[i] = i.ToString();
  6.     dTable.Rows.Add(s);
  7. }
  8.  
To:
Expand|Select|Wrap|Line Numbers
  1. s = sLine.Split('|');
  2. DataRow row = dTable.NewRow();
  3. for (int i = 0; i <= s.Count() - 1; i++)
  4. {
  5.     row[i] = s[i].ToString();
  6. }
  7. // You need to add the row, not the 's' array
  8. dTable.Rows.Add(row);
  9.  
Feb 15 '13 #10
M1kkelZU
80 64KB
ok thanks, now the only thing its saying is
Expand|Select|Wrap|Line Numbers
  1. Can't find column 1
Any idea what could be going on?
Feb 15 '13 #11
Mikkeee
94 64KB
Which line of code is giving you that error?
Feb 15 '13 #12
M1kkelZU
80 64KB
I have no clue, Its the Exception MessageBox that says that.
Feb 15 '13 #13
Mikkeee
94 64KB
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.
Feb 15 '13 #14
M1kkelZU
80 64KB
Ah ok thanks :) Atleast its working that 1% extra.
Feb 15 '13 #15
Mikkeee
94 64KB
Just out of curiosity, do you have any columns defined in your GridView? If so, remove them and let the datatable take care of them.
Feb 15 '13 #16
M1kkelZU
80 64KB



Is what I have. What would I have to remove then.
Feb 15 '13 #17
Mikkeee
94 64KB
OK, then you need to be sure that the columns in your datatable match the columns in your GridView. To do this you should eat the header record and define the columns.

Remove:
Expand|Select|Wrap|Line Numbers
  1. string[] s = sLine.Split(';');
  2. for (int i = 0; i <= s.Count() - 1; i++)
  3. {
  4.     dTable.Columns.Add(String.Format("col{0}",i), typeof(string));
  5. }
  6.  
Define your data table columns:
Expand|Select|Wrap|Line Numbers
  1. dTable.Columns.Add("Date", typeof(DateTime));
  2. dTable.Columns.Add("Log Name", typeof(string));
  3. // Keep going. Define your remaining columns and data types.
  4.  
Feb 15 '13 #18
M1kkelZU
80 64KB
Yeah I just figured it out like 2 minutes before I saw your post. Thanks alot Mikkeee :)
Feb 15 '13 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hanse Davion | last post by:
Can anyone provide some insight on what this problem could be? I have searched the web, read forums, and all the installation documentation for the dotnetnuke feeware portal from asp.net. I am...
2
by: Nomen Nescio | last post by:
Hi I've been trying to add a control dynamically to my page in order to pass a value to some client side script. I've solved that problem with a different approach now but I'd still like to...
8
by: Xero | last post by:
When my program is launched, there is a textbox on the form. When the user enters a character into the textbox (TextChanged), a second, declared textbox is added using this block of code. Dim...
3
by: Imran | last post by:
Hi, How can I persist a webpart that is added to a page using the webpart managers addWebPart method in asp.net 2.0. My intention is to give the user the ability to load a predetermined...
8
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists...
2
by: Bruce | last post by:
I am starting my first C## project. In one of my menu events I have the following: UserProfile winProfile = new UserProfile(); winProfile.Parent = this; winProfile.Show(); UserProfile is...
0
by: ANGanley | last post by:
Can anyone help me this? I have a class. Public Class db_Vehicle Public bs_VehicleDetails As New BindingSource() Public da_VehicleDetails As New SqlDataAdapter() Public table...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
2
by: freddie007 | last post by:
I would like to know 1. why my database cannot be added beyond record 74797 is it because I have set a wrong field type for my key field??? I have set my key field as int 30 2. and also...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.