0

这个问题的出现有些大但逻辑很简单所以请阅读一次。我使用以下代码在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);
   //        }


   //    }
   //}

    }

然后我得到了输出

在此处输入图像描述

当我在上面的程序中删除评论标签时,我得到了类似的输出

在此处输入图像描述

为什么没有显示科学、社会、泰卢固语科目。在此先感谢..

我的 HTML 代码是:

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

2 回答 2

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>

阅读此处了解有关 GridView 的更多信息。

于 2013-08-02T16:10:40.337 回答
0

你绑定

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

这是来自第二个 sql 命令的数据,只有“qr”

于 2013-08-02T16:08:55.647 回答