473,765 Members | 1,956 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
18 4620
M1kkelZU
80 New Member
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 New Member
Which line of code is giving you that error?
Feb 15 '13 #12
M1kkelZU
80 New Member
I have no clue, Its the Exception MessageBox that says that.
Feb 15 '13 #13
Mikkeee
94 New Member
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 New Member
Ah ok thanks :) Atleast its working that 1% extra.
Feb 15 '13 #15
Mikkeee
94 New Member
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 New Member



Is what I have. What would I have to remove then.
Feb 15 '13 #17
Mikkeee
94 New Member
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 New Member
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
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
1470
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
5351
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
1386
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
1994
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
24476
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
3320
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
1691
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
1372
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
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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
10007
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
9835
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
8832
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...
0
6649
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.