各位大神好,小弟现在要做一个小功能,例如用户输入一组随机数据,个数不定,如“111112212121111122222”这样的一组数据,通过拆分,放到datatagridview里面去,每列的行数只是4个而已,如果相邻的数超过了4个,那么就在后一行补足,效果如下图

解决方案 »

  1.   

     private void button1_Click(object sender, EventArgs e)
            {
                string numberStr = textBox1.Text.Trim();   //接收用户输入的数据
                string temp = string.Empty;               //定义变量temp
                string column = string.Empty;             //定义变量column
                List<string> lstColumn = new List<string>();            DataTable table = new DataTable();         //定义一个临时table
                DataColumn dcolumn;                        //定义列
                DataRow row;                               //定义行             row = table.NewRow();
                table.Rows.Add(row);                        numberStr.ToCharArray().ToList().ForEach((char c) =>
                {
                    if (string.IsNullOrEmpty(temp))
                    {
                        temp = c.ToString();
                    }                if (temp == c.ToString())
                    {
                        column += c;
                    }
                    else
                    {
                        lstColumn.Add(column);
                        temp = c.ToString();
                        column = temp;
                    }
                });
                lstColumn.Add(column);
                //遍历LstColumn  为table的列赋值            int id = 0;
                foreach (var item in lstColumn)
                {
                    id++;
                    if (item.Length <= 4)      //判断item的长度
                    {
                        dcolumn = new DataColumn();
                        dcolumn.ColumnName = id.ToString();
                        table.Columns.Add(dcolumn);
                    }
                    else
                    {
                        dcolumn = new DataColumn();
                        dcolumn.ColumnName = id.ToString();
                        table.Columns.Add(dcolumn);
                    }
                }
                //遍历列,对列中的数据赋值到行中
                foreach (var item in lstColumn)
                {
                    int i = 0;//                foreach (var subItem in item)
                    {
                        int j = 0;
                        table.Rows[j][i] = subItem.ToString();
                        j++;
                    }
                    i++;            }           
                dg.DataSource = table;        }    }
    }
                这是小弟自己写的源码,但是效果不出来,请各位大神指点,谢谢
      

  2.   

    给dt赋值你只要知道dt.row[i]["colname"]=....
      

  3.   

    111112212121111122222

    11111,22,1,2,12,11111,22222char [] c = 111112212121111122222.toCharArray();
    string out=null;
    string in = null;
    fro(int i = 0; i<c,lengthi++)
    {
    if(c==0)
    out=c[i]
    else 
    out=c[i-1];
    if(c[i]==out)
    in+=c[i]
    else
    in+=','
    }
    ---
    当前字符与过去的不一样就加一个逗号吧他们分开这时in就是11111,22,1,2,12,11111,22222
    ————
    string in[] = in.spilt(',')
    这时in就是11111][22][1][2][12][11111][22222]
    然后每个数组取得模数
    in[i],lengthi%4
    1为例子,得到1
    这时候你的单元就是当前列加1列,赋值就写循环吧手写的我意思就这样,你在改一下,有时间给你写全
      

  4.   

    in[i],lengthi%4
    1为例子,得到1
    如果in[i].length % 4  有余数呢?那么就该怎么加?
      

  5.   

     private void button1_Click(object sender, EventArgs e)
            {
                string numberStr = textBox1.Text.Trim();   //接收用户输入的数据
                string temp = string.Empty;               //定义变量temp
                string column = string.Empty;             //定义变量column
                List<string> lstColumn = new List<string>();            DataTable table = new DataTable();         //定义一个临时table
                DataColumn dcolumn;                        //定义列
                DataRow row;                               //定义行             row = table.NewRow();
                table.Rows.Add(row);            numberStr.ToCharArray().ToList().ForEach((char c) =>
                {
                    if (string.IsNullOrEmpty(temp))
                    {
                        temp = c.ToString();
                    }                if (temp == c.ToString())
                    {
                        column += c;
                    }
                    else
                    {
                        lstColumn.Add(column);
                        temp = c.ToString();
                        column = temp;
                    }
                });
                lstColumn.Add(column);
                //遍历LstColumn  为table的列赋值
                int id = 0;
                foreach (var item in lstColumn)
                {
                    id++;
                    dcolumn = new DataColumn();
                    dcolumn.ColumnName = id.ToString();
                    table.Columns.Add(dcolumn);
                    if (item.Length > 4)      //判断item的长度
                    {
                        int consult = 0;   //余数
                        int quotient = Math.DivRem(item.Length, 4, out consult);
                        if (consult != 0)
                        {
                            for (int k = 0; k < quotient; k++)
                            {
                                id = id + k;
                            }
                            id++;
                            dcolumn = new DataColumn();
                            dcolumn.ColumnName = id.ToString();
                            table.Columns.Add(dcolumn);
                        }
                        else
                        {
                            dcolumn = new DataColumn();
                            dcolumn.ColumnName = id.ToString();
                            table.Columns.Add(dcolumn);
                        }
                    }
                }
                //遍历列,对列中的数据赋值到行中            int i = 0;
                foreach (var item in lstColumn)
                {
                    int j = 0;
                    foreach (var subItem in item)
                    {
                        table.Rows[j][i] = subItem.ToString();
                        row = table.NewRow();
                        table.Rows.Add(row);                    if (j == 3)
                        {
                            j = 0;
                            i++;
                        }
                        else j++;
                    }
                    i++;
                }
                dg.DataSource = table;
            }
    代码我修改了,然后现在出现了一个问题,  相邻的数字 如果输入相同的数超过了8个,就会报错  错误信息是:无法找到列 2