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: - 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.
18 4614
I would instead create and populate a datatable and then set the datasource of your GridView to the datatable.
Hmm. I'll have a look at that. Where would I place the right statements etc then?
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. -
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.
Place the row into the datatable after you're done filling the row with values.
And you need to set the datasource when you're done. -
dataGridView1.DataSource = dt;
-
Ok thanks, only thing is I can't use the
as it gives me an error that I don't have row declared and can't use - DataRow row = new DataRow();
as it starts whining that it can't convert DataRow to String.
You do need to declare your DataRow first but you missed the [i] part after the row;
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: - 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 :/
Please re-post your modified 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.
You have a few lines of code inside a for loop that should be outside the for loop.
Change: -
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: -
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);
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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....
|
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
|
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
|
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..
|
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.
| |
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.
|
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()
|
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.
|
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
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |