zoukankan      html  css  js  c++  java
  • DataGridView控件添加数据时空白的可 错误情况

    写一个小程序,将数据库中的两张表相关信息显示在DataGridView中。代码如下:

     //获取项目数据,添加到表中
                SqlConnection con = new SqlConnection(MainForm2.connection);
                con.Open();
                string cmd1 = string.Format(@"Select * from ProjectInfo where ProjectName='{0}'", pjnm);
                string cmd2 = string.Format(@"Select * from ParameterInfo where ProjectName='{0}'", pjnm);
                //添加基本信息表
                SqlCommand cmd = new SqlCommand(cmd1,con);
                SqlDataReader reader = cmd.ExecuteReader();
                reader.Read();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    this.dataGridViewX1.Rows.Add();
                    this.dataGridViewX1[0, i].Value = reader.GetName(i);
                    if (reader.GetValue(i) == null)
                    {
                        this.dataGridViewX1[1, i].Value = "未输入";
                    }
                    else
                    {
                        this.dataGridViewX1[1, i].Value = reader.GetValue(i).ToString();
                    }
                }
    
                int fcount = reader.FieldCount;
                reader.Close();
    
                //添加参数信息
                SqlCommand cmdd = new SqlCommand(cmd2, con);
                reader = cmdd.ExecuteReader();
                reader.Read();
    
                for (int i = 1; i < reader.FieldCount; i++)
                {
                    this.dataGridViewX1.Rows.Add();
                    string str = ParameterInfoCls.standardName(i);
                    this.dataGridViewX1[0, fcount + i].Value = str;
                    if (reader.GetValue(i) == null || reader.GetValue(i).ToString() == "")
                    {
                        this.dataGridViewX1[1, fcount + i].Value = "未输入";
                    }
                    else
                    {
                        this.dataGridViewX1[1, fcount + i].Value = reader.GetValue(i).ToString();
                    }
                }
                reader.Close();   
    
                con.Close();

    运行后一直出现空白。如图:

    第一张表添加正常,第二张表却一直出现空白。断点逐步检查,每行代码值都是正常,且各字段的长度都是很大,数据远没有超出长度。

    多次试验发现,第二张表循环错误。由于第二张表与第一张表的第一个字段一样,所以为了剔除该行,循环变量初值为1。对DataGridView单元格赋值时,忘记对该初值减去1,造成

    在每次循环中添加第i行,而对第i+1行单元格赋值的情况。

    修正循环变量初值,即可。代码如下:

    for (int i = 1; i < reader.FieldCount; i++)
                {
                    this.dataGridViewX1.Rows.Add();
                    string str = ParameterInfoCls.standardName(i);
                    this.dataGridViewX1[0, fcount + i-1].Value = str;
                    if (reader.GetValue(i) == null || reader.GetValue(i).ToString() == "")
                    {
                        this.dataGridViewX1[1, fcount + i-1].Value = "未输入";
                    }
                    else
                    {
                        this.dataGridViewX1[1, fcount + i-1].Value = reader.GetValue(i).ToString();
                    }
                }
  • 相关阅读:
    理想团队模式构建的设想以及对软件流程的理解
    一、环境搭建 之 Windows10 安装 python3.5.2
    Codeforces1176A(A题)Divide it!
    Codeforces1144A(A题)Diverse Strings
    Codeforces1144B(B题)Parity Alternated Deletions
    Codeforces1144C(C题)Two Shuffled Sequences
    Codeforces1144D(D题)Equalize Them All
    Codeforces1157A(A题)Reachable Numbers
    Codeforces1157B(B题)Long Number
    Codeforces1141E(E题)Superhero Battle
  • 原文地址:https://www.cnblogs.com/DayDreamEveryWhere/p/3408810.html
Copyright © 2011-2022 走看看