我生成数字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; 消息:索引超出范围。必须是非负数且小于集合的大小。 参数名称:index
感谢您的时间:)
答案 0 :(得分:5)
保存自己的一些工作和.NET Framework的use the built-in features:
// automatically pads the number with up to 8 leading zeros
cTag = numTag.ToString("X8");
答案 1 :(得分:2)
您对嵌套 i
使用相同的变量for
。
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