猿问

如何使用字符串数组获取 datagridview 中每个页面的数字流动数字

我有一个包含 1000 多个项目的字符串数组,我已将其拆分为 4 个单独的数组。我试图将它们按datagridview数字顺序排列,每列可以容纳 28 个项目,每页有四列。


每个页面应该看起来像这样:


column 1 1-28

column 2 29-56

column 3 57-84

column 4 85-112

这将是第 1 页。第 2 页将从第 1 列的编号 113 开始。


目前,我已经将字符串数组放入其中,datagridview但数字在每个页面中不能正确流动,但它可以正确地直接向下流动。


将大数组分成四个部分:


string[] array = new string[listBox1.Items.Count];

for (int i = 0; i < listBox1.Items.Count; i++)

{

    array[i] = listBox1.Items[i].ToString();

}


string[] array2 = array.Take((array.Length / 2).ToArray();

string[] array3 = array.Skip((array.Length / 2).ToArray();


string[] array4 = array2.Take((array2.Length + 1) / 2).ToArray();

string[] array5 = array2.Skip((array2.Length + 1) / 2).ToArray();


string[] array6 = array3.Take((array3.Length + 1) / 2).ToArray();

string[] array7 = array3.Skip((array3.Length + 1) / 2).ToArray();

将数组添加到datagridview:


string a = "";

for (int i = 0; i < array4.Length; i++)

{

    addData(array4[i], a, array5[i], a, array6[i],a, array7[i],a );

}

添加数据的函数datagrid


private void addData(string a, string b, string c, string d, string e, string f, string g, string h)

{

    string[] row = { a, b, c, d,e,f,g,h };

    dataGridView1.Rows.Add(row);

}

我想要的结果是在每一页上都有数字流动的数字。


一只斗牛犬
浏览 91回答 1
1回答

慕标琳琳

下面将数字放入数据表中。您可以将表格设为datagridview1.DataSource = dt;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;namespace ConsoleApplication1{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //create a 1000 strings from numbers starting at 1001&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] array = Enumerable.Range(1001, 1000).Select(x => "String : " + x.ToString()).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var pages = array.Select((x,i) => new {str = x, index = i})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(x => x.index&nbsp; / 112)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(x => x.ToArray())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var rows = pages&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SelectMany(x => x.GroupBy(y => y.index % 28)).Select(y => y.ToArray())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Col A", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Col B", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Col C", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Columns.Add("Col D", typeof(string));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var row in rows)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataRow newRow = dt.Rows.Add(new object[] {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row[0].str,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (row.Length > 1) ? (object)row[1].str : null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (row.Length > 2) ? (object)row[2].str : null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (row.Length > 3) ? (object)row[3].str : null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答