I have a text file with data which might look something like:
UserName
Address
PostCode
PhoneNumber
The first four elements of the textfile belongs to one user, the next four to next user etc. I want to read from a textfile, and display data of each user. Reading and distinguishing data of each user is fine.
The problem is how would I display the data as such? I would like to display the data as a table or something along the line, where each user has a row. Say I want to display data like;
Name - Address - Postcode - PhoneNumber
Matt - 15 The - PO30 78 - 088997655
Mike - 16 The - PO31 78 - 088998955
If I was using a database I guess you can easily display it with a GridView, is there anyway to display it after reading from textfile?
Many Thanks,
Mike
EDIT:
I copied the code you gave, dragged a gridView to the page. Changed it's ID to dataGridView1
DataTable table = new DataTable();
table.Columns.Add("UserName", typeof(string));
table.Columns.Add("Address", typeof(string));
table.Columns.Add("PostCode", typeof(string));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("LastName", typeof(string));
dataGridView1.DataSource = table;
table.Rows.Add(Label8.Text, Label4.Text, Label5.Text, Label6.Text, Label7.Text);
You can load your data into a DataTable and bind a gridview to the DataTable just as you would if it were coming from from a database. Something like this:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Address", typeof(string));
table.Columns.Add("Postcode", typeof(string));
table.Columns.Add("PhoneNumber", typeof(string));
table.Rows.Add("Matt", "15 The", "PO30 78", "088997655");
dataGridView1.DataSource = table;
dataGridView1.DataBind();
Related
Snippet of the excel table
As can be seen in the Width column, there are some irregular values like "H:15 ,T:5, W:8.5"
For cells like 59cmand 18.50cm I can just remove the cm when reading into the datagrid so that's fine
When reading the excel table into the datagridview, I set the datagridview column for "Width" as double
Snippet of my working code
bomTable = new DataTable("BomTable");
bomTable.Columns.Add("Description", typeof(string));
bomTable.Columns.Add("Vendor", typeof(string));
bomTable.Columns.Add("PartName", typeof(string));
bomTable.Columns.Add("Weight", typeof(double));
bomTable.Columns.Add("WeightUnit", typeof(string));
bomTable.Columns.Add("Width", typeof(double)); //set as double
bomTable.Columns.Add("Width Unit", typeof(string));
Naturally, I cannot assign those irregular values into the Width Column as it is, so is it possible to change its datatype based on a condition? Like for example
Example Code
if (excelRange.Cells[fabricHeaderRow + 1, j].Value2.Contains("H") ||
excelRange.Cells[fabricHeaderRow + 1, j].Value2.Contains("T") ||
excelRange.Cells[fabricHeaderRow + 1, j].Value2.Contains("W"))
{
bomTable.Columns["Width"].DataType = typeof(string);
}
If there's any other workaround, I'd welcome them too
so my problem is that I want to display a table inside another table in WPF.
I use a DataTable in order to display some data and there is one column, in which I need to display another DataTable. I set AutoGenerateColumns="True". For a little testing, this is what I wrote (well, it works as expected):
var curDataTable = new DataTable();
curDataTable.Columns.Add("name" , typeof(string));
curDataTable.Columns.Add("number", typeof(int));
DataRow curRowData = curDataTable.NewRow();
curRowData[0] = "jones";
curRowData[1] = 90;
curDataTable.Rows.Add(curRowData);
Now, let's say I already have a filled DataTable _dataTable. I now want to display this _dataTable in my second column. This is what i would expect to work, but what does not work:
var curDataTable = new DataTable();
curDataTable.Columns.Add("name" , typeof(string));
curDataTable.Columns.Add("table", typeof(DataTable));
DataRow curRowData = curDataTable.NewRow();
curRowData[0] = "jones";
curRowData[1] = _dataTable;
curDataTable.Rows.Add(curRowData);
Has anyone an idea how to fix this?
Using nested DataTables won't work.
What you should do is to replace the outer DataTable with a custom type and bind to a custom collection property of this type in a DataGridTemplateColumn.
I don't think that nested DataTables are possible...
What you can do is create new column in '_datatable' with the name of "name"
_datatable.Columns.Add("name" , typeof(string)).SetOrdinal(0);
Using SetOrdinal(0) you can make "name" column first column. Now you maybe able to add new columns to it.
I hope it helps
I am not getting datatable I created, in report viewer. I am getting report parameter in report viewer but not table. Please help me to resolve this issue. Thanks in advance.
DataTable dt = new DataTable("Type-1");
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
dt.Columns.Add("Column4", typeof(string));
dt.Columns.Add("Column5", typeof(string));
dt.Rows.Add("0.4", "1", "3.0","0.95-1.0-1.05", "Test1");
dt.Rows.Add("7.0", "1", "3.0","1.68-1.76-1.85", "Test2");
var reportDataSource1 = new ReportDataSource("Type-1", dt);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc";
reportViewer1.LocalReport.SetParameters(new ReportParameter("RP_SrNo", _SrNo));
reportViewer1.LocalReport.SetParameters(new ReportParameter("RP_TestType", _TestType));
reportViewer1.LocalReport.SetParameters(new ReportParameter("RP_Date", _Date));
this.reportViewer1.RefreshReport();
you can try this with a Dataset. Follow the steps below
1. Add a new Dataset to your project.
2. Go to the Dataset and a new Datatable.
3. On the DataTable right click to add columns
4. Right on the columns to define the properties such dataTypes
5. Go to your report designer to add the Dataset you just created.
6. From the report Data panel right click on DataSet then Add DataSet.
7. Give you DataSet a name i.e DataSet1 -> Select datasource to the dataset you created earlier -> Available Datasets select the dataTable you added.
8. Design report -> Insert a table on your report and choose the data to be displayed.
9. Now your code should look like below.
//Report Source
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc";
//Set Parameters
reportViewer1.LocalReport.SetParameters(new ReportParameter("RP_SrNo", _SrNo));
reportViewer1.LocalReport.SetParameters(new ReportParameter("RP_TestType", _TestType));
reportViewer1.LocalReport.SetParameters(new ReportParameter("RP_Date", _Date));
//Prepare datasource (These should be the same columns as on step 3)
DataTable dt = new DataTable("Type-1");
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column3", typeof(string));
dt.Columns.Add("Column4", typeof(string));
dt.Columns.Add("Column5", typeof(string));
dt.Rows.Add("0.4", "1", "3.0","0.95-1.0-1.05", "Test1");
dt.Rows.Add("7.0", "1", "3.0","1.68-1.76-1.85", "Test2");
//Add dataset as defined in step 7
var reportDataSource1 = new ReportDataSource("DataSet1", dt);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.RefreshReport();
Hope that helps.
I am trying to add column wise data in C#, for e.g.
I have four columns in datatable i.e. No, productoriginal, productmedium, productmedium and productlarge.
first i want to fill ABC then i want to No then Product then productoriginal then productmedium and productlarge
below is my code
DataTable dt = new DataTable();
dt.Columns.Add("No", typeof(Int));
dt.Columns.Add("productoriginal", typeof(string));
dt.Columns.Add("productmedium", typeof(string));
dt.Columns.Add("productlarge", typeof(string));
how do i proceed further ?
If you're looking to add rows to the DataTable, there is a good example on MSDN, but the basics of it is this:
DataRow row = dt.NewRow();
row["No"] = "Value for No here";
row["productoriginal"] = "Value for productoriginal here";
dt.Rows.Add(row);
Take a look into MSDN here.
As mentioned, this is just an example, I'm sure you can amend it as you need to.
My chart series are really near by each other and this is a problem. I don't know why they are grouped, they look like : http://i39.tinypic.com/wks007.png or http://blogs.msdn.com/blogfiles/alexgor/WindowsLiveWriter/DataBindingMSChartcontrol_10712/image_2.png, but must be separate :(
I want to have each bar with his own name without grouping.
Like on second image, Andrew has 5 Columns near by each other with one title: "Andrew", but I want to have them separately)
How I can do this?
I use the MS chart library
P.S. excuse me for my english :)
My code example:
var table = new DataTable();
s = chart1.Series.Add("Ser1");
s.XValueMember = "Col1";
s.YValueMembers = "Col1_Count";
s = chart1.Series.Add("Ser2");
s.XValueMember = "Col2";
s.YValueMembers = "Col2_Count";
s = chart1.Series.Add("Ser3");
s.XValueMember = "Col3";
s.YValueMembers = "Col3_Count";
table.Columns.Add("Col1", typeof(string));
table.Columns.Add("Col1_Count", typeof(int));
table.Columns.Add("Col2", typeof(string));
table.Columns.Add("Col2_Count", typeof(int));
table.Columns.Add("Col3", typeof(string));
table.Columns.Add("Col3_Count", typeof(int));
Then i execute SqlCommand SELECT and add rows to table and then
chart1.DataBind();
firstly, be sure you put the Charttype as Column in chart control like ChartType="Column",
then, the XValueMember should be the same column, and Bindpoints to series with seperate dataview, like
DataView sDt1 = table .DefaultView.ToTable(true, new[] { "Column_Identifier", "RecordCount" }).DefaultView;
Series[0].Points.DataBindXY(sDt1, "Column_Identifier", sDt1, "RecordCount");