0

我生成数字 8,9,A,B (Int64)

但是我需要

00000008

00000009

0000000A

0000000B

000001ED

此代码:

        int count = 1;
        sb = new StringBuilder();
        sb.Append("SELECT max(Numb) FROM tblAs");

        string sql = sb.ToString();
        cmd.CommandText = sql;
        cmd.CommandType = CommandType.Text;
        cmd.Connection = Conn;
        count = (int)cmd.ExecuteScalar();
        int newCount = count;
        int i;


        for (i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                newCount = newCount + 1;

                Int64 numTag;
                string cTag = Convert.ToString(newCount);
                numTag = Int64.Parse(cTag);
                cTag = numTag.ToString("X");

                if (cTag.Length < 8)
                {
                    int countchar = 8 - cTag.Length;
                    for (i = 1; i <= countchar; i++)
                    {
                        cTag = "0" + cTag;
                        dataGridView1.Rows[i].Cells[3].Value = cTag;
                    }
                }
            }

错误行: dataGridView1.Rows[i].Cells[3].Value = cTag; 消息:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

谢谢你的时间:)

4

2 回答 2

5

节省一些工作并使用 .NET Framework 的内置功能

// automatically pads the number with up to 8 leading zeros
cTag = numTag.ToString("X8");  
于 2012-09-26T09:40:38.943 回答
2

i您对嵌套 for的 s使用相同的变量。

for (i = 0; i < dataGridView1.Rows.Count; i++)

for (i = 1; i <= countchar; i++)

这就是为什么你得到Index was out of range. Must be non-negative and less than the size of the collection

于 2012-09-26T09:38:57.120 回答