【发布时间】:2012-09-26 09:36:46
【问题描述】:
我生成数字 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; 消息:索引超出范围。必须是非负数且小于集合的大小。 参数名称:索引
感谢您的宝贵时间:)
【问题讨论】:
-
行索引从零开始,而不是从 1
-
总的来说,我认为您应该熟悉调试工具。大多数时候,
just stepping into code, watching variables and comparing expected values and actual values in your head可以解决问题。通过这样做,您会惊讶地发现代码中的所有错误如此之快。
标签: c# winforms c#-4.0 if-statement for-loop