Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a GridView which is used to display report of Profit Amount. Here i'm binding GridView from backent using DataTable my code is like this

C#
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "GetProfitAmount";
cmd.Connection = _sConn;
cmd.CommandType = CommandType.StoredProcedure;
if (shareHolderId != 0)
{
    cmd.Parameters.Add("@ShareHolderId", SqlDbType.Int).Value = shareHolderId;
}
else
{
    cmd.Parameters.Add("@ShareHolderId", SqlDbType.Int).Value = DBNull.Value;
}
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);


here i'm binding directly from code behind. There I've two columns, which are "ActualPaid and PaidAmount" Now I want to add one more column to existing DataTable with the column name TotalProfit and value will be like "ActualPaid - PaidAmount".

So can anyone help me please

Thanks in @dv@nce
Posted
Updated 19-Aug-13 21:40pm
v2

1 solution

Hi,

try like below:
C#
dt.Columns.Add("TotalProfit", type(System.Double));
dt.AcceptChanges();

foreach(DataRow row in dt.Rows)
{
    row["TotalProfit"] = Convert.ToDouble(row["ActualPaid"].ToString()) - Convert.ToDouble(row["PaidAmount"].ToString());
}
dt.AcceptChanges();


you must take care of casting and default values.

hope it helps.
 
Share this answer
 
Comments
Aboobakkar Siddeq D U 20-Aug-13 4:43am    
Thanks,,,but it showing error like this "the name commandtype does not exist in the current context"
Aboobakkar Siddeq D U 20-Aug-13 4:49am    
Thanks a lot, it works fine now

I used this code

<pre lang="cs">
dt.Columns.Add("TotalProfit");
dt.AcceptChanges();
foreach (DataRow row in dt.Rows)
{
row["TotalProfit"] = Convert.ToDouble(row["PaidAmount"].ToString()) - Convert.ToDouble(row["ActualPaid"].ToString());
}
dt.AcceptChanges();</pre>
Karthik Harve 20-Aug-13 5:54am    
you welcome !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900