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.
Related
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 need to set a column's value in a datatable as the sum of other two columns in a datatable.I tried expression column and is working fine . But the problem is that the value of expression column is not getting updated always.Suppose A,B are two columns in the datatable and the sum of these two columns should be shown in Column 'C'.When when the value of B is changed then the value of columns 'C' is also getting changed.But not when column 'A' is changed
DataTable dt1 = new DataTable();
dt1.Columns.Add("A", typeof(decimal));
dt1.Columns.Add("B", typeof(decimal));
dt1.Columns.Add("C", typeof(decimal), "A+B");
myGridView.Datasource = dt1;
This is an educated guess without seeing your code but it seems as though if you are using Compute then you quite possibly shouldn't use an expression column for this, rather you should use a DataColumn... Taken from here
"If you must perform an operation on two or more columns, you should create a DataColumn, set its Expression property to an appropriate expression, and use an aggregate expression on the resulting column. In that case, given a DataColumn with the name "total", and the Expression property set to this: "Quantity * UnitPrice"
Should go like this:
DataTable dt1 = new DataTable();
dt1.Columns.Add("A", typeof(decimal));
dt1.Columns.Add("B", typeof(decimal));
dt1.Columns.Add("C", typeof(decimal));
foreach (DataRow dr in dt1.Rows)
{
dr[2] = Convert.ToDecimal(dr[0]) + Convert.ToDecimal(dr[1]);
}
myGridView.Datasource = dt1;
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");
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();
Is there anyway to store row header information in a datatable so that when i bind it to a datagridview, it will automatically display both the column and row headers in c#?
Linqpad Demo-Program
As far as i understood you would like to add the column name as values into the datatable / the datagridview. The following is a Linqpad-Program you can easily copy paste into Linqpad to play around. The code adds the column-names to the first row to the datatable. You can easily bind this datatable to a gridview - but beware that each column of the datatable must be of type string.
void Main()
{
GetDataTable().Dump();
}
public DataTable GetDataTable()
{
var dt = new DataTable();
dt.Columns.Add("Id", typeof(string)); // dt.Columns.Add("Id", typeof(int));
dt.Columns["Id"].Caption ="my id";
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Job", typeof(string));
dt.Rows.Add(GetHeaders(dt));
dt.Rows.Add(1, "Janeway", "Captain");
dt.Rows.Add(2, "Seven Of Nine", "nobody knows");
dt.Rows.Add(3, "Doctor", "Medical Officer");
return dt;
}
public DataRow GetHeaders(DataTable dt)
{
DataRow dataRow = dt.NewRow();
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
columnNames.Dump();
dataRow.ItemArray = columnNames;
return dataRow;
}
Update 2019-06 with additional explanation and alternative code
The method GetHeaders is not the simplest option to get the headers.
Previoulsy the extension method Cast<TResult>(IEnumerable) was used on the DataColumnCollection-Class An alternative would be to just iterate over the collection - this what is done In GetHeadersNew T
public DataRow GetHeadersNew(DataTable dt)
{
DataRow row = dt.NewRow();
DataColumnCollection columns = dt.Columns;
for (int i = 0 ;i <columns.Count ;i++)
{
row[i] = columns[i].ColumnName;
}
return row;
}
This is likely more efficient because less objects and methods are involved.
As long as you can create them with the code based on the data in the row I would just add them at run time using c#. Add a column to the datatable and run through it with a foreach loop. As long as there are not too many rows this code will execute very quickly:
DataTable dt = new DataTable();
// code here to get your datatable
dt.Columns.Add("rowheader");
foreach (DataRow r in dt.Rows)
{
r["rowheader"] = "my nice row header";
}
Then output the new column rowheader as the first cell in the grid.
Another solution is to use the sql query to return an 'extra' column in the result set. for example:
Select *, 'my nice row header' as rowheader from myTable
In this way you make SQL do all the work.