Gridview列未正确显示

时间:2013-08-02 15:55:02

标签: asp.net

这个问题的出现是一些大而逻辑很简单所以请一读。我使用下面的代码在gridview中显示输出。

protected void Button1_Click(object sender, EventArgs e)
{
    int colcount = 0;
    MySqlConnection conMySQL = new MySqlConnection(connString);
    conMySQL.Open();

    string k = "select subject from class_subject where standard='" + DropDownList3.SelectedItem.Value+"';";
    MySqlDataAdapter da = new MySqlDataAdapter(k, conMySQL);
    DataTable dt = new DataTable();
    da.Fill(dt);

    StringBuilder sb = new StringBuilder();
    Session["columncount"] = dt.Columns.Count;

   for (int count = 0; count < dt.Rows.Count; count++)
    {
        sb.Append("'0' as '");
        sb.Append(dt.Rows[count][0].ToString());
        sb.Append("'");

        if (count < dt.Rows.Count - 1)
        {
            sb.Append(",");

        }
        colcount++;

    }
    //Label1.Text = sb.ToString();

    string qr = "select Admission_num,name,'"+DropDownList3.SelectedItem.Text+"' as Standard,'"+DropDownList1.SelectedItem.Text+"' as Fyear,'"+DropDownList2.SelectedItem.Text+"' as Type_Of_Exam," + sb.ToString() + " from student_data where Admission_Num='" + txt1.Text + "' and Completed_status='running';";
    MySqlCommand cmd = new MySqlCommand(qr, conMySQL);
    StringBuilder sb1 = new StringBuilder();
    MySqlDataAdapter ada = new MySqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    ada.Fill(ds);

    GridView1.DataSource = ds;
   GridView1.DataBind();
   //foreach (GridViewRow g1 in GridView1.Rows)
   //{
   //    TextBox TextBox1 = new TextBox();
   //    for (int j = 0; j < 11; j++)
   //    {
   //        //GridView1.Rows[0].Cells[j].Controls.Add(TextBox1);
   //        //g1.Cells[j].Controls.Add(TextBox1);
   //        if (j > 4)
   //        {
   //            TableCell cell = g1.Cells[j];
   //            cell.Controls.Clear();

   //            //create a textbox and add it to the cell
   //            TextBox txt = new TextBox();
   //            txt.Text = cell.Text;
   //            cell.Controls.Add(txt);
   //        }


   //    }
   //}

    }

然后我得到了像

这样的输出

enter image description here

当我在上面的程序中删除评论标签时。我得到了像

这样的输出

enter image description here

为什么科学,社交,泰卢固语科目没有出现。谢谢提前..

我的Html代码是:

<asp:GridView ID="GridView1" runat="server" ViewStateMode="Disabled" 
   >
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

你绑定

GridView1.DataSource = ds;
GridView1.DataBind();

来自第二个sql命令的数据仅为“qr”

答案 1 :(得分:0)

您必须在GridView中添加列,如:

<asp:GridView ID="GridView1" Width="100%" AutoGenerateColumns="False" runat="server" />
            <Columns>
                <asp:TemplateField ItemStyle-Width="120px" ControlStyle-BackColor="Control">
                    <ItemTemplate>
                            <asp:Label ID="lblMath" runat="server" Text='<%# Eval("Math") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lblEnglish" runat="server" Text='<%# Eval("English") %>'></asp:Label>
                        </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

阅读 here ,了解有关GridView的更多信息。