简体   繁体   English

具有数据列的动态gridview

[英]dynamic gridview with data columns

i am using gridview control which having i Have to insert the gridview columns dynamic i insert a row values like 我正在使用gridview控件,我必须要动态插入gridview列,然后插入一行值

i Have to insert the gridview columns dynamic i insert a row values like 我必须动态插入gridview列

string[,] mularray = {
                      {"F_1","F_2","F_3","F_4","F_5","F_6"}
                     };

foreach (string names in mularray)
{
    for (int i = 0; i < 1; i++)
    {                   
        dt.Rows[dt.Rows.Count - 1]["10:00-11:00"] = mularray[i, 0];
        dt.Rows[dt.Rows.Count - 1]["11:00-12:00"] = mularray[i, 1];
        dt.Rows[dt.Rows.Count - 1]["12:00-01:00"] = mularray[i, 2]; 
        dt.Rows[dt.Rows.Count - 1]["02:00-03:00"] = mularray[i, 3];
        dt.Rows[dt.Rows.Count - 1]["03:00-04:00"] = mularray[i, 4];
        dt.Rows[dt.Rows.Count - 1]["04:00-05:00"] = mularray[i, 5];     
    }   
}

when i use this but it is inserting in last row like i shown in picture 当我使用它但它像我在图片中所示插入最后一行时

next rows values should increment like F-2,F-3,F_4,F_5,F_6,F_1 then what have to do like this 下一行的值应该像F-2,F-3,F_4,F_5,F_6,F_1一样递增F-2,F-3,F_4,F_5,F_6,F_1然后该做什么

If I am understanding you correctly, you should be able to do something as simple as the following: 如果我对您的理解正确,那么您应该可以执行以下操作:

string[,] mularray =
{
    {"F_1","F_2","F_3","F_4","F_5","F_6"},
    {"F_2","F_3","F_4","F_5","F_6","F_1"},
    {"F_3","F_4","F_5","F_6","F_1","F_2"},
    {"F_4","F_5","F_6","F_1","F_2","F_3"},
    {"F_5","F_6","F_1","F_2","F_3","F_4"},
    {"F_6","F_1","F_2","F_3","F_4","F_5"}
};

for (int i = 0; i < mularray.GetLength(0); i++)
{
    for (int j = 0; j < mularray.GetLength(1); j++)
    {
        dt.Rows[i]["10:00-11:00"] = mularray[j, 0];
        dt.Rows[i]["11:00-12:00"] = mularray[j, 1];
        dt.Rows[i]["12:00-01:00"] = mularray[j, 2];
        dt.Rows[i]["02:00-03:00"] = mularray[j, 3];
        dt.Rows[i]["03:00-04:00"] = mularray[j, 4];
        dt.Rows[i]["04:00-05:00"] = mularray[j, 5];
    }
}

This iterates through mularray in both dimensions. mularray在两个维度上遍历mularray It assigns each row of mularray (defined by iterator i ) to DataTable dt . 它将mularray每一行(由迭代器i定义)分配给DataTable dt It uses your original loop (redefined with iterator j ) which assigns values to each of the cells within the current row. 它使用原始循环(用迭代器j重新定义),该循环将值分配给当前行中的每个单元格。

As well, if you are only trying to input a single line and automatically shift elements to the right during each iteration, this can be simplified. 同样,如果您仅尝试输入单行并在每次迭代期间自动将元素向右移,则可以简化此过程。 You could use the modulo operator ( % ) to cause the elements to loop around, with an offset to select the correct value. 您可以使用模运算符( % )导致元素循环,并使用偏移量选择正确的值。

string[] mularray = { "F_1", "F_2", "F_3", "F_4", "F_5", "F_6" };

for (int i = 0; i < 6; i++)
{
        dt.Rows[i]["10:00-11:00"] = mularray[i];
        dt.Rows[i]["11:00-12:00"] = mularray[(i + 1) % 6];
        dt.Rows[i]["12:00-01:00"] = mularray[(i + 2) % 6];
        dt.Rows[i]["02:00-03:00"] = mularray[(i + 3) % 6];
        dt.Rows[i]["03:00-04:00"] = mularray[(i + 4) % 6];
        dt.Rows[i]["04:00-05:00"] = mularray[(i + 5) % 6];
}

In this case the mularray has been changed to a single dimension string array, because both dimensions are not necessary. 在这种情况下, mularray已更改为一维string数组,因为两个维度都是不必要的。 Now the elements are selected by the calculated index, using (i + 1) % 6 to represent the pattern of {1, 2, 3, 4, 5, 0} . 现在,通过计算的索引选择元素,使用(i + 1) % 6表示{1, 2, 3, 4, 5, 0} Likewise the next element can be selected by (i + 2) % 6 which represents {2, 3, 4, 5, 0, 1} . 同样,可以通过(i + 2) % 6选择下一个元素,该元素代表{2, 3, 4, 5, 0, 1} The first element does not require use of modulo because it uses i , which already represents {0, 1, 2, 3, 4, 5} . 第一个元素不需要使用模,因为它使用i ,它已经表示{0, 1, 2, 3, 4, 5}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM