datatable不返回正确的行和列 [英] datatable not return correct row and column

查看:84
本文介绍了datatable不返回正确的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友

以下是我根据行和列生成数据表的代码

Hi friends
The following is my code for generating datatable based on the rows and columns

DataTable result= userdao.getdateanddayfortimetable1(comp);
            DataTable  period = userdao.getperiodfortimetable();
            int periodcount = period.Rows.Count;
            int daycount = result.Rows.Count;
            //System.Windows.Forms.TableLayoutPanel panel = new System.Windows.Forms.TableLayoutPanel();
            //panel.Controls.Clear();
            //panel.ColumnStyles.Clear();
            //panel.RowStyles.Clear();
            //panel.ColumnCount = periodcount;
            //panel.RowCount = daycount;
            DataTable dt = new DataTable();
            for (int i = 0; i < daycount; i++)
            {
                dt.Rows.Add();
                for (int j = 0; j <periodcount; j++)
                {
                    dt.Columns.Add();
                }
            }
            
            //for (int k = 0; k < periodcount; k++)
            //{
            //    grid.Columns[k].HeaderText = period.Rows[k]["PeriodNo"].ToString(); //+"(" + period.Rows[k]["StartTime"].ToString() + period.Rows[k]["EndTime"].ToString() + ")";
            //}

            return dt;
            
        }







这里的periodcount是列值,daycount是行值(例如periodcount = 7和daycount = 4),但是在执行for循环后,它返回4行,其中28列为datatable.what'我的代码错了。帮助我解决问题。

谢谢




Here periodcount is the columnvalue and the daycount is the row value(for example periodcount=7 and daycount=4),but after executing for loop it return 4rows with 28 column for datatable.what''s wrong with my code. help me to fix the problem.
Thank you

推荐答案

为什么每次都要添加行和列?

表需要列,必须设置一次。因此,将内部(列)循环移到外部(行)循环之外。



然后,您可以在添加列后添加行:

Why are you adding rows and columns each time?
A table needs columns, which must be set once. So move your inner (columns) loop outside the outer (rows) loop.

You can then add the rows once you have added the columns:
DataTable result = userdao.getdateanddayfortimetable1(comp);
DataTable period = userdao.getperiodfortimetable();
int periodcount = period.Rows.Count;
int daycount = result.Rows.Count;
DataTable dt = new DataTable();
for (int j = 0; j < periodcount; j++)
    {
    dt.Columns.Add();
    }
for (int i = 0; i < daycount; i++)
    {
    dt.Rows.Add();
    }
return dt;


这篇关于datatable不返回正确的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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