在GridView中未正确设置数据

时间:2019-05-30 07:47:36

标签: c# asp.net gridview

我正在尝试从数据库中检索数据并在gridview中进行支付。为了创建标题,我正在使用HeaderTemplet标记。但是在gridview中,员工ID的第一列始终为空。

我的代码在aspx页面下面:

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" Width="99%" GridLines="Both" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>

                            <tr>


                                <th style="padding: 2.5px; width: 10%;" >eid</th>


                                <th style="padding: 2.5px; width: 55%;" >First Name</th>
                                <th style="padding: 2.5px;" >Last Name</th>

                        </HeaderTemplate>

                    </asp:TemplateField>

                    <asp:BoundField DataField="idemp" />
                    <asp:BoundField DataField="fname" />
                    <asp:BoundField DataField="lname" />

<asp:TemplateField>

                    <ItemTemplate>

                        <asp:GridView ID="GridView2" runat="server" Width="99%" GridLines="Both" AutoGenerateColumns="false" CssClass="ChildGrid">

                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
</form>

后面的代码:

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page

    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Populate the GridView. 
                bindGridview();
            } 

        }

        public void bindGridview()
        {
            DataTable dt = new DataTable();
            string constr = @"server=127.0.0.1;user id=root;pwd=n0711p2010p;database=emp";
            using (MySqlConnection con = new MySqlConnection(constr))
            {
                MySqlCommand cmd = new MySqlCommand("select idemp,fname,lname from emp", con);
                con.Open();
                MySqlDataReader dtreader = cmd.ExecuteReader();
                dt.Load(dtreader);
                if (dt.Rows.Count >0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    con.Close();

                }
            }

        }

        //RowDataBound Event  
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //Checking the RowType of the Row  
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataTable dt = new DataTable();

                string constr = @"server=127.0.0.1;user id=root;pwd=n0711p2010p;database=emp";
                using (MySqlConnection con = new MySqlConnection(constr))
                {
                    con.Open();
                    GridView child_gridview = (GridView)e.Row.FindControl("GridView2");
                    String CountryId = (e.Row.RowIndex+1).ToString();
                    MySqlCommand cmd = new MySqlCommand("select salary,post from emp where idemp="+CountryId, con);


                    MySqlDataReader dtreader = cmd.ExecuteReader();
                    dt.Load(dtreader);
                    if (dt.Rows.Count > 0)
                    {
                        child_gridview.DataSource = dt;
                        child_gridview.DataBind();

                    }
                }
            }
        }

    }
}

,输出如下:Gridview

gridview的第一列始终为空。使用HeaderTemplet标签时

2 个答案:

答案 0 :(得分:1)

如果要嵌套的GridView跨越多列,则必须在父GridView的RowDataBound中执行此操作。您可以在其中设置colspan并删除最后2个单元格。

所以,如果您有GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>

                <%# Eval("Id") %>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>

                <%# Eval("Name") %>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Saldy">
            <ItemTemplate>

                <asp:GridView ID="NestedGrid" runat="server" AutoGenerateColumns="true">
                </asp:GridView>

            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address">
        </asp:TemplateField>
        <asp:TemplateField HeaderText="City">
        </asp:TemplateField>
    </Columns>
</asp:GridView>

还有RowDataBound事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a normal datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the row back to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //use findcontrol to locate the nested gridview
        GridView gv = e.Row.FindControl("NestedGrid") as GridView;

        //bind data to the nested grid
        gv.DataSource = source;
        gv.DataBind();

        //set the column span to 3 on the cell that has the nested gridview
        e.Row.Cells[2].ColumnSpan = 3;

        //hide the last 2 cells
        e.Row.Cells[3].Visible = false;
        e.Row.Cells[4].Visible = false;
    }
}

唯一的问题是父级和子级的单元格宽度不匹配,因此您可能需要为这些列设置固定宽度。

答案 1 :(得分:0)

似乎您正在检索数据就很好,但是显示不正确。

您误用了TemplateField来定义标题。

您可以简单地执行以下操作:

<asp:GridView ID="GridView1" runat="server" Width="99%" GridLines="Both" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                <Columns>


                <asp:BoundField DataField="idemp" headertext="Employee ID" />
                <asp:BoundField DataField="fname" headertext="First Name" />
                <asp:BoundField DataField="lname" headertext="Last Name" />


        </Columns>
    </asp:GridView>

如您所见,标题名称是在绑定列中定义的。

享受,让我知道是否还有问题