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);
}
Related
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?
There is some data in my datagridview. I want to insert all that data at once into database from datagridview using LINQ. But, it couldn't be inserted.
Here is my code :
DetailTransaction dt = new DetailTransaction();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dt.TransactionID = labelID.Text;
dt.ProductID = dataGridView1.Rows[i].Cells[0].Value.ToString();
dt.Quantity = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
}
dc.DetailTransactions.InsertOnSubmit(dt);
dc.SubmitChanges();
Can anyone tell me what is the correct code?
You need to populate a collection of DetailTransaction and then use InsertAllOnSubmit like:
List<DetailTransaction > list = new List<DetailTransaction>();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DetailTransaction dt = new DetailTransaction();
dt.TransactionID = labelID.Text;
dt.ProductID = dataGridView1.Rows[i].Cells[0].Value.ToString();
dt.Quantity = int.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString());
list.Add(dt);
}
dc.DetailTransactions.InsertAllOnSubmit(list);
dc.SubmitChanges();
With your current code, you will end up with a single row inserted in the database and that row will be holding the records of the last row in dataGridView1
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();
How to use Datarow to retrieve data based on column name? I am trying to loop the db data from my first looping
//Trying to get data
DataRow dr = dsResult.Tables[1].Rows[0];
//trying to get data successful
//what i trying to achieve is to retrieve data from database based on rows index and
column name
for(int i =0; i <datagridview.Rows.Count ; i++){
string a = dr['ColumnName'].['RowsIndex'].toString(); //Failed
}
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow dr = dataTable.Rows[i]; //Where the RowIndex
string a = dr[0].ToString(); //Where the ColumnIndex or ColumnName
}
Try ?
dr.Rows[RowsIndex]['ColumnName'].ToString()
Have you tried removing the period after the 'columnName' specification, and putting rows[i]["columnName"] rather than [columnName][rows]?
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!