473,748 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rows Cannot be Programmaticall y added.

80 New Member
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 4614
Mikkeee
94 New Member
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 New Member
Hmm. I'll have a look at that. Where would I place the right statements etc then?
Feb 15 '13 #3
Mikkeee
94 New Member
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 New Member
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 New Member
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 New Member
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 New Member
Please re-post your modified code.
Feb 15 '13 #8
M1kkelZU
80 New Member
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 New Member
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

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

Similar topics

2
2133
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 very confident that all the security settings have been configured correctly. My setup is a Windows XP 5.1, MSDE SP3, and VS2003. ----------------------------- Server Error in '/DotNetNuke' Application....
2
1467
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 know what was going on with my first method. On first load the following example will add MyTextBox to the placeholder and some JavaScript which knows the field ID to look for can read its content. So far so good. However, after a PostBack I'd like
8
5349
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 textbox2 As New TextBox Controls.Add(textbox2) When the second textbox is filled with character as well, a third textbox is added using smilar method as adding the second one. However, when I add the
3
1383
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 collection of web parts by selecting a preset. Thanks in advance..
8
1993
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 of three dropdowns and seven text boxes. When the button is clicked, I add another control to the page in Click event of the button and populate the three dropdowns. The text boxes are to be populated by the user.
2
24473
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 a form, this is a MDI form.
0
3317
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 As New DataTable()
13
1688
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 Then I get an error that udUpperLim1 is "Not Defined" so I cannot get the value in the control.
2
1371
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 the last few data added appeared and then disappeared
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8252
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.