在Gridview中编辑命令

时间:2016-10-12 09:13:30

标签: c# asp.net

我在gridview中从数据库中提取数据,在那里我给出了一个链接按钮“编辑”来编辑该行的数据。但是在编辑时,我的gridview的一列(YEAR)变为下拉列表。从数据库中获取数据时,列“年”由标签组成,在编辑模式下,它将更改为下拉列表。此外,我添加了一个条件,使链接按钮根据标签“年”的值显示/消失。当我编辑特定行时,它会抛出一个错误,因为只要调用edit命令,它就会将列转换为下拉列表,但row_databound会搜索label。它显示“对象未设置为实例.....”PLease帮助

Aspx

 <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource2" OnRowDataBound="gv1_RowDataBound" OnRowUpdating="gv1_RowUpdating" DataKeyNames="tid" CssClass="auto-style1" Width="694px">
        <Columns>
             <asp:TemplateField HeaderText="Sr No">
                 <ItemTemplate>
                     <%#Container.DataItemIndex+1 %>
                 </ItemTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="tid" SortExpression="tid" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("tid") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Title" SortExpression="title">
                <EditItemTemplate>
                    <asp:TextBox ID="txtTitle1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblTitle" runat="server" Text='<%# Bind("title") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Category" SortExpression="category">
                <ItemTemplate>
                    <asp:Label ID="lblCategory" runat="server" Text='<%# Bind("category") %>'></asp:Label></ItemTemplate>
                <EditItemTemplate>
                    <asp:RadioButtonList ID="rbtnlist" runat="server"><asp:ListItem Text="Soft Skills"></asp:ListItem><asp:ListItem Text="Technical Skills"></asp:ListItem></asp:RadioButtonList>
                </EditItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Announced at" SortExpression="dt">                     
                 <ItemTemplate>
                     <asp:Label ID="lblDt" runat="server" Text='<%# Bind("dt", "{0:dd/MMM/yyyy HH:mm tt}") %>'></asp:Label>
                 </ItemTemplate>
             </asp:TemplateField>
           <asp:TemplateField HeaderText="Year" SortExpression="year">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlyr" runat="server"></asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblyr" runat="server" Text='<%# Bind("year") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="User Id" SortExpression="userid">                     
                <ItemTemplate>
                    <asp:Label ID="lblUid" runat="server" Text='<%# Bind("userid") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>                        
             <asp:TemplateField ShowHeader="False">
                 <ItemTemplate>
                     <asp:LinkButton ID="btndel" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClientClick="return isConfirm()" Visible="false"></asp:LinkButton>                                               

                        <%--<asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>--%>

                      </ItemTemplate>                       
             </asp:TemplateField>           
            <asp:TemplateField ShowHeader="False">
                 <EditItemTemplate>
                     <asp:LinkButton ID="btnupdte" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                     &nbsp;<asp:LinkButton ID="btncncl" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                 </EditItemTemplate>
                 <ItemTemplate>
                     <asp:LinkButton ID="btnedit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" Visible="false"></asp:LinkButton>
                 </ItemTemplate>
             </asp:TemplateField>            
             <asp:TemplateField ShowHeader="false">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnmail" runat="server" CausesValidation="False" OnClientClick="return isConfirm()" Text="Send Mail" Visible="false"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>                  
            </Columns> 
        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
        <PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC" />
        <RowStyle BackColor="White" ForeColor="#003399" />
        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <SortedAscendingCellStyle BackColor="#EDF6F6" />
        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
        <SortedDescendingCellStyle BackColor="#D6DFDF" />
        <SortedDescendingHeaderStyle BackColor="#002876" />
    </asp:GridView>

C#

 protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lbly = (Label)e.Row.FindControl("lblyr");
            if (lbly.Text == DateTime.Now.Year.ToString() || lbly.Text == ((DateTime.Now.Year)-1).ToString())
            {

                LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
                LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
                LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
                btdel.Visible = true;
                btedt.Visible = true;
                btsm.Visible = true;

            }
            if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
                RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");                  
                DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
                for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year)-1) ; i--)
                {
                    ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                ddlist.DataBind();
                rbtnlist.DataBind();
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

我假设您使用colum name&#34; Year&#34;将您的网格绑定到DataTable。尝试以下代码,其中dt指的是您的数据表:

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int rowno = e.Row.DataItemIndex;
            //Suppose dt is DataTable to which you are binding your grid
            //and year is the column name within this datatable
            string year = Convert.ToString(dt.Rows[e.Row.DataItemIndex]["year"]);
            if (year == DateTime.Now.Year.ToString() || year == ((DateTime.Now.Year) - 1).ToString())
            {

                LinkButton btedt = (LinkButton)e.Row.FindControl("btnedit");
                LinkButton btdel = (LinkButton)e.Row.FindControl("btndel");
                LinkButton btsm = (LinkButton)e.Row.FindControl("btnmail");
                btdel.Visible = true;
                btedt.Visible = true;
                btsm.Visible = true;

            }
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                RadioButtonList rbtnlist = (RadioButtonList)e.Row.FindControl("rbtnlist");
                DropDownList ddlist = (DropDownList)e.Row.FindControl("ddlyr");
                for (int i = (DateTime.Now.Year); i >= ((DateTime.Now.Year) - 1); i--)
                {
                    ddlist.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                ddlist.DataBind();
                rbtnlist.DataBind();
            }
        }