为什么gridview是空的而不是从数据库加载?

时间:2015-02-17 18:11:05

标签: c# asp.net gridview

我从商店程序填充数据库但它不起作用,即使调试器没有命中。有什么问题,为什么它是空白的?我已经尝试了几次没有调试但没有调试它。但

protected void GridViewConductorsDevices_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            DataTable ConductorDevicesInfo = new DataTable();

            int ID = Convert.ToInt32(GridViewConductorsDevices.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            ViewState["ID"] = ID;
            ConductorDevicesInfo = (DataTable)ManageTransport.ManageConductorDevices.GetAssignedDevices();
            this.TextBoxID.Text = GridViewConductorsDevices.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
            this.DropDownListConductors.SelectedValue= ConductorDevicesInfo.Rows[0]["Conductor_ID"].ToString();
            this.DropDownListDevices.SelectedValue = ConductorDevicesInfo.Rows[0]["Device_ID"].ToString();

            MultiView1.ActiveViewIndex = -1;
            MultiView1.ActiveViewIndex = 2;
        }
        catch (Exception)
        {

        }
        finally
        {
        }
    }

的.aspx

<asp:GridView ID="GridViewConductorsDevices" runat="server" AutoGenerateColumns="False" CaptionAlign="Top"
                                CssClass="table table-hover table-striped table-bordered" DataKeyNames="ID" OnRowCommand="GridViewConductorsDevices_RowCommand"
                                Width="100%">
                                <Columns>
                                    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Conductor_ID" Visible="false" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Device_ID" Visible="false" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Conductor" HeaderText="Conductor" 
                                        ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                    <asp:BoundField DataField="Terminal" HeaderText="Terminal" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>
                                   <%-- <asp:BoundField DataField="TotalDistance" HeaderText="Total Distance" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>--%>
                                    <%-- <asp:BoundField DataField="Model" HeaderText="Model" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>--%>
                                    <%--<asp:BoundField DataField="ChassisNo" HeaderText="Chassis No" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>--%>
                                    <%--<asp:BoundField DataField="EngineNo" HeaderText="EngineNo" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop">
                                        <HeaderStyle HorizontalAlign="Left" />
                                    </asp:BoundField>--%>
                                    <asp:ButtonField Text="Edit" HeaderText="Option" ItemStyle-CssClass="visible-desktop"
                                        HeaderStyle-CssClass="visible-desktop"></asp:ButtonField>
                                </Columns>
                            </asp:GridView>

1 个答案:

答案 0 :(得分:0)

您的Gridview,<asp:GridView ID="GridViewConductorsDevices" runat="server" >需要一个数据源。

您提到了存储过程,但在您发布的代码中没有引用它。

要将数据绑定到Gridview,您需要在后面的代码中为GridView DataSource属性分配一个,然后调用GridViewConductorsDevices.DataBind()

或者,您可以将DataSource控件的ID提供给Gridview DataSourceID属性,该属性将在页面生命周期内自动绑定。

但是,在任何一种情况下,事件 GridViewConductorsDevices_RowCommand 都不会被调用,因为您尚未定义CommandField来触发行事件。