如何将变量分配给DataTable [英] How to Assign Variable to DataTable

查看:98
本文介绍了如何将变量分配给DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int mark1,mark2,total;

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



现在我有了变量total,并将此变量分配给了Datatable.



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);



这是静态的.

如果我通过输入的for(i)n循环动态获取此mark1,mark2输入

我如何动态地向此数据表分配变量.

MainTotal
3
4
11
45
就像动态地



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

推荐答案

请参阅此MSDN文章.

如何:将行添加到数据表 [ ^ ]

应该是这样的:

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);
}


您可以尝试使用 DataTable.ColumnChanged事件 [ ^ ].
EventHandler [ ^ ]检查mark1 mark2的值,将它们相加并将其放入totals column.
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;
}

希望它会有所帮助:)


嗨prabhu
使用此查询,而不是将列添加到数据表中


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.


这篇关于如何将变量分配给DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆