InsertAt Method Problem

  • Thread starter Thread starter Kelvin
  • Start date Start date
K

Kelvin

Hi All,

I have some question about inserting new row into datatable.
I don't know why my all new rows inserted into end of datatable.
I am using myTable.Rows.InsertAt(dr, 0);

I don't know what is mean of the pos parameter. I also found problem
from MSDN.

row
The DataRow to add.
pos
The location in the collection where you want to add the DataRow.

If the value specified for the pos parameter is greater than the
number of rows in the collection, the new row is added to the end.

Please advise.
 
Kelvin said:
I have some question about inserting new row into datatable.
I don't know why my all new rows inserted into end of datatable.
I am using myTable.Rows.InsertAt(dr, 0);

I don't know what is mean of the pos parameter. I also found problem
from MSDN.

row
The DataRow to add.
pos
The location in the collection where you want to add the DataRow.

If the value specified for the pos parameter is greater than the
number of rows in the collection, the new row is added to the end.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hi All,

I don't why I can't insert new rows between rows, and it can append on
buttom record of datatable. With reference my code, Using Rows.InserAt
or Rows.Add() is the better. Please advise.

// Define DataTable Format
DataRow dr2;
DataRow dr;
DataView myDataView;

if ( myTable.Rows.Count > 0 )
{
msg.Text = "";
y = myTable.Rows.Count;
for (i=0; i < y ; i++ )
{
DataRow dataRow = myTable.Rows;

if (myOrderNo != (String)dataRow["OrderNo"])
{
mySubTotal = Convert.ToDouble(myTotalPrice) - (Convert.ToDouble
(myTotalPrice) * (Convert.ToInt16(dataRow["HeadDiscount"])/100));
dr = myTable.NewRow();
DataRowCollection rc = myTable.Rows;
dr["CustNo"] = (String)dataRow["OrderNo"];
dr["TotalPrice"] = mySubTotal;
// myTable.Rows.InsertAt(dr,i);
myTable.Rows.Add(dr);
myTable.AcceptChanges();
myGrandTotal = myGrandTotal + Convert.ToDouble(mySubTotal);
myOrderNo = (String)dataRow["OrderNo"];
mySubTotal = 0;
myTotalPrice = 0;
}
else
{
myTotalPrice = myTotalPrice +
Convert.ToDouble(dataRow["TotalPrice"]) ;
}

}

dr2 = myTable.NewRow();
dr2["CustNo"] = "Grand Total : ";
dr2["TotalPrice"] = myGrandTotal;

myTable.Rows.Add(dr2);
myTable.AcceptChanges();

DetailGrid.DataSource = myTable;
 
Back
Top