Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
int mark1,mark2,total;

total=mark1+mark2;     Ex://total=1+2; total=3;


Now i have variable total and i assigned this variable to Datatable.

C#
DataTable dtttt = new DataTable();
                                        dtttt.Columns.Add("MainTotal");
                                        DataRow dr;
                                        dr = dtttt.NewRow();
                                        dr[0] = total;
                                        dtttt.Rows.InsertAt(dr, 0);


This is static..

if i get this mark1,mark2 input dynamically through for loop(i.e) n of input

how can i assign variable to this datatable dynamically.

MainTotal
3
4
11
45
like dynamically
Posted
Updated 10-Jul-12 5:31am
v5
Comments
[no name] 10-Jul-12 11:47am    
I guess that I do not understand what your isssue is. You would assign the value of a variable exactly like you are. Perhaps you are confused about the meaning of static and dynamic?
sandeep nagabhairava 11-Jul-12 8:39am    
select (select (m1+m2+m3)from marks)as Total,m1,m2,m3 from marks
NaniCh 12-Jul-12 7:14am    
nice sandeep

Look at this MSDN article.

How to: Add Rows to a DataTable [^]

Should be something like this:

foreach(int total in marks)
{
	DataRow row = dtttt.NewRow();
	row["MainTotal"] = total.ToString();
	
	dtttt.Rows.Add(newCustomersRow);
}
 
Share this answer
 
Comments
Prasad_Kulkarni 13-Jul-12 0:11am    
Good one +5!
Manas Bhardwaj 13-Jul-12 3:43am    
thx!
You could try using the DataTable.ColumnChanged Event[^].
In the EventHandler[^] check the values of mark1 and mark2, add them and put them into the totals column.
C#
private void Form1_Load(object sender, EventArgs e)
{
    // Create a DataTable with a few columns.
    DataTable dt = new DataTable();
    DataColumn colMark1 = new DataColumn("mark1", typeof(int));
    dt.Columns.Add(colMark1);
    DataColumn colMark2 = new DataColumn("mark2", typeof(int));
    dt.Columns.Add(colMark2);
    DataColumn colTotal = new DataColumn("total", typeof(int));
    dt.Columns.Add(colTotal);
    dt.ColumnChanged += (s, ea) =>
        {
            // colTotal will be changed, this check prevents StackOverflow.
            if (ea.Column != colTotal)
            {
                int mark1 = 0;
                int mark2 = 0;
                // Check the value for null and dbnull.
                object value1 = ea.Row[colMark1];
                if (value1 != null && value1 != DBNull.Value)
                {										{
                    // Cast the value of the cell to an int.
                    mark1 = Convert.ToInt32(value1); 
                }
                // Do the above again for mark2.
                object value2 = ea.Row[colMark2];
                if (value2 != null && value2 != DBNull.Value)
                { mark2 = Convert.ToInt32(value2); }
                // Add mark1 and mark2. This will change the total value.
                // Note that this change is not directly depicted in your DataGridView.
                // Try refreshing the grid or whatever if you wish to see the changed directly.
                ea.Row["total"] = mark1 + mark2;
            }
        };
    dataGridView1.DataSource = dt;
}
Hope it helps :)
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 13-Jul-12 0:11am    
Good answer +5
Sander Rossel 13-Jul-12 1:38am    
Thanks :)
hi prabhu
use this query instead of adding column to data table


SQL
"select(SELECT mark1, mark2,mark3 (mark1+ mark2+mark3) FROM Tablename C1) AS MainTotal,mark1, mark2,mark3 from Tablename C2"


best of luck.
 
Share this answer
 

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