Click here to Skip to main content
12,242,812 members (40,787 online)
Rate this:
 
Please Sign up or sign in to 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.

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 10-Jul-12 2:38am
HRVP357
Edited 10-Jul-12 6:31am
v5
Comments
Wes Aday 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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);
}
  Permalink  
Comments
Prasad_Kulkarni 13-Jul-12 0:11am
   
Good one +5!
Manas Bhardwaj 13-Jul-12 3:43am
   
thx!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
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 Smile | :)
  Permalink  
v2
Comments
Prasad_Kulkarni 13-Jul-12 0:11am
   
Good answer +5
Naerling 13-Jul-12 1:38am
   
Thanks :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

hi prabhu
use this query instead of adding column to data table


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

best of luck.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 11 Jul 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100