I have datatable in c#, I want to add two column value in one, Add means 2+2 =4 like that. please help me this my below code not working
for (int i = 0; i < dtOrdersDetail.Rows.Count; i++)
{
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtItemRow["FinalAmount"] = dtOrdersDetail.Rows[i]["ComboAmount"] + dtOrdersDetail.Rows[i]["TotalPrice"];
}
In this Case you have to correct the code in two place.
Cast the source data table Columns.
dtItemRow["FinalAmount"] = Convert.ToInt32(dtOrdersDetail.Rows[i]["ComboAmount"]) + Convert.ToInt32(dtOrdersDetail.Rows[i]["TotalPrice"]);
Add the row in data table.
dtOrderReceipt.Rows.Add(dtItemRow);
So in summary your code look likes as below
for (int i = 0; i < dtOrdersDetail.Rows.Count; i++)
{
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtItemRow["FinalAmount"] = Convert.ToInt32(dtOrdersDetail.Rows[i]["ComboAmount"]) + Convert.ToInt32(dtOrdersDetail.Rows[i]["TotalPrice"]);
dtOrderReceipt.Rows.Add(dtItemRow);
}
#john, look at the below logic
DataTable table = new DataTable();
table.Columns.Add("TotalPrice", typeof(int));
table.Columns.Add("ComboAmount", typeof(int));
// Here we add five DataRows.
table.Rows.Add(10,10);
table.Rows.Add(20,20);
table.Rows.Add(30,20);
DataTable dtOrderReceipt = new DataTable();
dtOrderReceipt.Columns.Add("FinalAmount", typeof(int));
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtOrderReceipt.Rows.InsertAt(dtItemRow, i);
dtItemRow["FinalAmount"] = (int)table.Rows[i]["ComboAmount"] + (int)table.Rows[i]["TotalPrice"];
}
I think after create a newrow you forgot to Inject it on your Datatable. i.e.
DataRow dtItemRow = dtOrderReceipt.NewRow();
dtOrderReceipt.Rows.InsertAt(dtItemRow, i);
Let me know is that logic works for you?
Related
I am new to C# programming and topic of operating with jagged array.
I have some data stored in my string[][] arrayname and want to show it in datagridview.
Will be very grateful if you could advice me on the case.
You need to create dataset, usually I use a DataTable, I have drafted a solution to your problem, but you have to using Linq:
var ListName = arrayname.ToList();
//get number of column, probalby you dont need it
int cols = ListName.Select(r => r.Length).Max();
//Create a datasource
DataTable dt = new DataTable();
//Write column, probalby you dont need it
for (int f = 0; f < cols; f++)
dt.Columns.Add("Col " + (f+1));
foreach (var row in ListName) {
//make a row
List<string> Lrow = new List<string>();
Lrow.AddRange(row);
//if row is too short add fields
if (Lrow.Count < cols)
for (int i = Lrow.Count; i < dt.Columns.Count; i++) {
Lrow.Add("");
}
//at last add row to dataTable
dt.Rows.Add(Lrow.ToArray());
}
//and set dataGridView's DataSource to DataTable
dataGridView1.DataSource = dt;
The result should be this
I have asked a question previously based on the errors of this code. However, after the suggestions given, there is no more error. However, the data from the row in queue table would not move to the the missedQueue table.
I'm not sure why it won't work :(
this is my code:
DataSet queue = DBMgr.GetDataSet("SELECT * FROM queue");
DataTable missedQueue = queue.Tables[0].Clone();
DataRow dr = queue.Tables[0].NewRow();
for (int i = 0; i < queue.Tables[0].Columns.Count; i++)
{
dr[queue.Tables[0].Columns[i].ColumnName] = queue.Tables[0].Rows[0][i];
}
missedQueue.Rows.Add(dr.ItemArray);
}
Your DataRow should be of missedQueue table and add the row inside loop like
DataRow dr = null;
for (int i = 0; i < queue.Tables[0].Columns.Count; i++)
{
dr = missedQueue.NewRow();
dr[queue.Tables[0].Columns[i].ColumnName] = queue.Tables[0].Rows[0][i];
missedQueue.Rows.Add(dr);
}
I have a datatable containing over 100 columns, how ever I need to strip out all columns
except first 11 columns.
I need to retain data of 1st 11 columns.
I am doing it with following code
public DataTable validdatatable(DataTable table)
{
DataTable dt = new DataTable();
for (int i = 0; i < 11; i++)
{
DataColumn dc = new DataColumn();
dc.ColumnName = table.Columns[i].ColumnName;
dc.DataType = table.Columns[i].DataType;
dt.Columns.Add(dc);
}
for (int i = 0; i < table.Rows.Count; i++)
{
object[] ob = table.Rows[i].ItemArray;
...
...
}
return dt;
}
This methods works but is too heavy on CPU and Ram.
Is there any other method with which I can proceed?
Try this:
public DataTable validdatatable(DataTable table)
{
var dt = table.Columns.Cast<DataColumn>().Take(11);
return dt.CopyToDataTable();
}
Or Something like this. It will give you at least a way to work on it.
Note that You need to add a reference to the assembly: System.Data.DataSetExtensions.dll then you can write your function like above.
You can try this. The only difference would be instead of object[] ob = table.Rows[i].ItemArray it will just grab the first 11 columns using the index and make an array out of that (itemArray will make an array of all 100 columns). Still doubt this will solve your memory issues if you are that tight but it's probably worth a shot.
var copyDt = new DataTable();
for (var i = 0; i < 11; i++)
{
copyDt.Columns.Add(dataTable.Columns[i].ColumnName, dataTable.Columns[1].DataType);
}
copyDt.BeginLoadData();
foreach (DataRow dr in dataTable.Rows)
{
copyDt.Rows.Add(Enumerable.Range(0, 11).Select(i => dr[i]).ToArray());
}
copyDt.EndLoadData();
my DataTable has over 1000 columns and I want to display values on the datagridview. Because of the FillWeigth problem I use the following method to fill the gridview,
public bool TransferDataTableToGrid(DataGridView dataGrid, DataTable dataTable)
{
dataGrid.SuspendLayout();
if ((dataGrid != null) && (dataTable != null))
{
dataGrid.Columns.Clear();
dataGrid.AutoGenerateColumns = false;
dataGrid.DataSource = dataTable;
for (int i = 0; i < dataTable.Columns.Count; i++)
{
DataGridViewColumn column = new DataGridViewColumn();
column.Name = dataTable.Columns[i].ColumnName;
column.FillWeight = 1;
column.CellTemplate = new DataGridViewTextBoxCell();
column.ValueType = dataTable.Columns[i].DataType;
dataGrid.Columns.Add(column);
}
for (int i = 0; i < dataTable.Columns.Count; i++)
{
for (int ii = 0; ii < dataTable.Rows.Count; ii++)
{
dataGrid[i, ii].Value = dataTable.Rows[ii][i];
}
}
}
dataGrid.ResumeLayout();
return true;
}
and sometimes I have an effect that my gridview is empty. Only after second execution data is displayed. Do you have any ideas, why...?
Thanks.
I recommend to use paging, i mean that you can show about 20 columns with navigation buttons
under your grid, it's like Google or others... even your are not programming a web application.
Use binding source to fill your grid
SqlDataAdapter adapter = new SqlDataAdapter(database.cmd);
dataSet1.Tables.Clear();
adapter.Fill(dataSet1, "Table");
bs = new BindingSource();
bs.DataSource = dataSet1.Tables["Table"];
dataGridView1.DataSource = bs;
now you dont need to worry about creating columns and fill cells in loops and its much better performance
Bind Data to Datagridview
Well, I solved my problem. With Ivan's suggestion I tried the alternative way to fill data: instead of using DataSource I add new rows manually
foreach (DataRow row in dataTable.Rows)
{
var dataGridRow = new DataGridViewRow();
dataGridRow.CreateCells(dataGrid);
for (int i = 0; i < row.ItemArray.Length; i++)
{
dataGridRow.Cells[i].Value = row.ItemArray[i];
}
dataGrid.Rows.Add(dataGridRow);
}
...and it works - data in dgv is displayed. Thanks!
I've been trying to generate a table with n number of rows. Being used to PHP makes this all the worst. I tried the following code:
using System.Data;
// Create a DataTable instance
DataTable dTbl = new DataTable("myDynamicTable");
// Create a DataColumn instances
DataColumn dValue = new DataColumn();
DataColumn dMember = new DataColumn();
dValue.ColumnName = "Id";
dValue.DataType = Type.GetType("System.Int32");
dMember.ColumnName = "Name";
dMember.DataType = Type.GetType("System.String");
// Add these DataColumns into the DataTable
dTbl.Columns.Add(dValue);
dTbl.Columns.Add(dMember);
DataRow myrow = dTbl.NewRow();
myrow["Id"] = 1;
myrow["Name"] = "Tux";
// Add the row into the table
dTbl.Rows.Add(myrow);
but nothing displayed. Any idea why?
All I need is to display a table with 3 columns and n number of rows. This number will of rows will be dependent on number of records in database satisfying a certain conditions.
I also tried this:
HtmlTable table1 = new HtmlTable();
// Set the table's formatting-related properties.
table1.Border = 1;
table1.CellPadding = 3;
table1.CellSpacing = 3;
table1.BorderColor = "red";
// Start adding content to the table.
HtmlTableRow row;
HtmlTableCell cell;
for (int i = 1; i <= 5; i++)
{
// Create a new row and set its background color.
row = new HtmlTableRow();
row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
for (int j = 1; j <= 4; j++)
{
// Create a cell and set its text.
cell = new HtmlTableCell();
cell.InnerHtml = "Row: " + i.ToString() +
"<br>Cell: " + j.ToString();
// Add the cell to the current row.
row.Cells.Add(cell);
}
// Add the row to the table.
table1.Rows.Add(row);
}
// Add the table to the page.
this.Controls.Add(table1);
but it didn't work!
Instead of doing "this.Controls.Add(table1)" add the table to the .aspx page, and then modify it through the code.
Even better - use a databound GridView.