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 4548
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);
-
ok thanks, now the only thing its saying is
Any idea what could be going on?
Which line of code is giving you that error?
I have no clue, Its the Exception MessageBox that says that.
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.
Ah ok thanks :) Atleast its working that 1% extra.
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.
Is what I have. What would I have to remove then.
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: -
string[] s = sLine.Split(';');
-
for (int i = 0; i <= s.Count() - 1; i++)
-
{
-
dTable.Columns.Add(String.Format("col{0}",i), typeof(string));
-
}
-
Define your data table columns: -
dTable.Columns.Add("Date", typeof(DateTime));
-
dTable.Columns.Add("Log Name", typeof(string));
-
// Keep going. Define your remaining columns and data types.
-
Yeah I just figured it out like 2 minutes before I saw your post. Thanks alot Mikkeee :)
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |