【问题标题】:gridview editing and updatinggridview 编辑和更新
【发布时间】:2013-07-26 06:45:10
【问题描述】:

我有一个gridview,并且有一个超链接列,上面写着更新。单击该页面后,将重定向到另一个页面,并且特定的行值将显示在另一个网格视图中。并且用户需要编辑 1 列并使其更新,然后在 gridview 外部有一个按钮发送或接受,单击它时应该生成邮件,它应该获取更新的网格值并将其发送给他的其他用户和页面应该被重定向到上一页。

gridview的aspx代码

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                     AutoGenerateColumns="false" BackColor="White" BorderColor="#0061C1" 
                     BorderStyle="None" CaptionAlign="Bottom" EmptyDataText="No Records Found" 
                     Font-Names="Verdana" Font-Size="X-Small" ForeColor="#0061C1" Height="70px" 
                     ShowFooter="True" ShowHeaderWhenEmpty="True" OnRowCommand="GridView1_RowCommand" 
                     OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing"
                     onselectedindexchanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating"
                     Width="796px">
                     <Columns>
                         <asp:BoundField DataField="LeaveID" Visible="false">
                         <FooterStyle BackColor="#0061C1" />
                         <HeaderStyle BackColor="#0061C1" ForeColor="White" HorizontalAlign="Center" 
                             VerticalAlign="Middle" />
                         <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10px" />
                         </asp:BoundField>
                         <asp:TemplateField HeaderText="Applied By">
                         <ItemTemplate>
                         <asp:Label 
                            ID="LoggedInUser" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="24px" Text='<%# Eval("LoggedInUser")%>' Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Begin Date">
                    <ItemTemplate>
                    <asp:Label 
                            ID="BeginDate" runat="server" DataFormatString="{0:dd/MM/yyyy}" 
                            Font-Names="Verdana" Text='<%# Eval("BeginDate","{0:dd/MM/yyyy}")%>' Font-Size="X-Small" Height="20px" 
                             Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="End Date">
                    <ItemTemplate>
                    <asp:Label 
                            ID="EndDate" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="20px" Text='<%# Eval("EndDate","{0:dd/MM/yyyy}")%>'  Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Num of Days">
                    <ItemTemplate>
                    <asp:Label 
                            ID="NumofDays" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="20px" Text='<%# Eval("NumofDays")%>' Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Type of Leave ">
                    <ItemTemplate>
                    <asp:Label 
                            ID="LeaveType" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            Height="20px" Text='<%# Eval("TypeofLeave")%>'  Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                    <asp:Label 
                            ID="Status" runat="server" Font-Names="Verdana" Font-Size="X-Small" 
                            ForeColor="Black" Height="20px" Text='<%# Eval("Status")%>'  Width="100px"></asp:Label>
                    </ItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                        <asp:TemplateField  HeaderText="Reason for Reject">
                        <ItemTemplate>
                        <asp:Label ID="RejectReason" 
                            runat="server" Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" 
                            Height="20px" Text='<%# Eval("RejectReason")%>' Enabled="true" Visible="true" Width="100px"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:TextBox ID="TxtRejectReason" 
                            runat="server" Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" 
                            Height="20px" Text='<%# Eval("RejectReason")%>' Enabled="true" Visible="true" Width="100px"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterStyle BackColor="#0061C1" />
                    <HeaderStyle BackColor="#0061C1" ForeColor="White"></HeaderStyle>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                    </asp:TemplateField>
                         <asp:BoundField DataField="LogdInUser" Visible="false" >
                         <FooterStyle BackColor="#0061C1" />
                         <HeaderStyle BackColor="#0061C1" ForeColor="White" HorizontalAlign="Center" 
                             VerticalAlign="Middle" />
                         <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                         </asp:BoundField>
                         <asp:BoundField DataField="Manager" Visible="false" >
                         <FooterStyle BackColor="#0061C1" />
                         <HeaderStyle BackColor="#0061C1" ForeColor="White" HorizontalAlign="Center" 
                             VerticalAlign="Middle" />
                         <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                         </asp:BoundField>
                         <asp:CommandField ShowEditButton="true"  ButtonType="Button"  EditText="Edit">
                <ControlStyle Width="50" />
            </asp:CommandField>
                     </Columns>
                 </asp:GridView>

我需要编辑 RejectReason 列。

cs页面

protected void Page_Load(object sender, EventArgs e)
    {
        MTMSDTO objc = new MTMSDTO();
        if (!IsPostBack)
        {
            int LeaveID = 0;
            int.TryParse(Request.QueryString["LeaveID"], out LeaveID);
            objc.LeaveID = LeaveID;
            objc.RejectReason = TxtRejectReason.Text;
            DataSet lapp = obj.GetLeaveApproved(objc);
            DataView LApp = new DataView();
            LApp.Table = lapp.Tables[0];
            GridView1.DataSource = LApp;
            GridView1.DataBind();

        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int LeaveID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

        TextBox TxtRejectReason = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TxtRejectReason");

        GridView1.EditIndex = -1;
        GridView1.DataBind();
    }

一旦我点击gridview中的编辑按钮,它就不会显示任何记录

请帮帮我

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    试试下面的代码:

    <asp:GridView ID="GridView1" runat="server"  Width = "550px"
             AutoGenerateColumns = "false" Font-Names = "Arial"
             Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 
             HeaderStyle-BackColor = "green" AllowPaging ="true"  ShowFooter = "true" 
             OnPageIndexChanging = "OnPaging" onrowediting="EditCustomer"
             onrowupdating="UpdateCustomer"  onrowcancelingedit="CancelEdit"
             PageSize = "10" >
             <Columns>
             <asp:TemplateField ItemStyle-Width = "30px"  HeaderText = "CustomerID">
                 <ItemTemplate>
                    <asp:Label ID="lblCustomerID" runat="server"
                    Text='<%# Eval("CustomerID")%>'></asp:Label>
      </ItemTemplate>
      <FooterTemplate>
            <asp:TextBox ID="txtCustomerID" Width = "40px"
            MaxLength = "5" runat="server"></asp:TextBox>
      </FooterTemplate>
      </asp:TemplateField><asp:TemplateField ItemStyle-Width = "100px"  HeaderText = "Name">
      <ItemTemplate>
        <asp:Label ID="lblContactName" runat="server"
                Text='<%# Eval("ContactName")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtContactName" runat="server"
            Text='<%# Eval("ContactName")%>'></asp:TextBox>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtContactName" runat="server"></asp:TextBox>
       </FooterTemplate>
       </asp:TemplateField>
       <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Company">
       <ItemTemplate>
       <asp:Label ID="lblCompany" runat="server"
            Text='<%# Eval("CompanyName")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <asp:TextBox ID="txtCompany" runat="server"
            Text='<%# Eval("CompanyName")%>'></asp:TextBox>
       </EditItemTemplate> 
       <FooterTemplate>
          <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
       </FooterTemplate>
       </asp:TemplateField>
       <asp:TemplateField>
       <ItemTemplate>
          <asp:LinkButton ID="lnkRemove" runat="server"
            CommandArgument = '<%# Eval("CustomerID")%>'
         OnClientClick = "return confirm('Do you want to delete?')"
        Text = "Delete" OnClick = "DeleteCustomer"></asp:LinkButton>
       </ItemTemplate>
       <FooterTemplate>
          <asp:Button ID="btnAdd" runat="server" Text="Add"
            OnClick = "AddNewCustomer" />
        </FooterTemplate>
       </asp:TemplateField>
       <asp:CommandField  ShowEditButton="True" />
       </Columns>
       <AlternatingRowStyle BackColor="#C2D69B"  />
       </asp:GridView>
    

    代码背后:

    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
            BindData();
       }
    }
    private void BindData()
    {
    }
    protected void AddNewCustomer(object sender, EventArgs e)
    {
             string CustomerID=((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
             string Name = ((TextBox)GridView1.FooterRow.FindControl("txtContactName")).Text;
             string Company = ((TextBox)GridView1.FooterRow.FindControl("txtCompany")).Text;
            //Your Code here...
     }
     protected void EditCustomer(object sender, GridViewEditEventArgs e)
     {
           GridView1.EditIndex = e.NewEditIndex;
            BindData();
     }
     protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
     {
          GridView1.EditIndex = -1;
          BindData();
      }
     protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
     {
          string CustomerID = ((Label)GridView1.Rows[e.RowIndex]
                        .FindControl("lblCustomerID")).Text;
          string Name = ((TextBox)GridView1.Rows[e.RowIndex]
                        .FindControl("txtContactName")).Text;
          string Company = ((TextBox)GridView1.Rows[e.RowIndex]
                        .FindControl("txtCompany")).Text;
          //Your code here...
      }
    
      protected void DeleteCustomer(object sender, EventArgs e)
      {
           LinkButton lnkRemove = (LinkButton)sender;
           SqlConnection con = new SqlConnection(strConnString);
           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "delete from  customers where " +
           "CustomerID=@CustomerID;" +
           "select CustomerID,ContactName,CompanyName from customers";
           cmd.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value
        = lnkRemove.CommandArgument;
         GridView1.DataSource = GetData(cmd);
         GridView1.DataBind();
      }
    

    【讨论】:

    • 感谢您的cmets,如果您喜欢答案,请点击“UP”
    • 非常感谢您的回答,感谢您对提问者的好意:-)
    • @Md.ParvezAlam 在我看来,答案是完美、正确且有效的。所以我们社区成员应该感谢答案。
    • 我得到同样的错误......(索引超出范围。必须是非负数并且小于集合的大小。参数名称:索引)
    【解决方案2】:

    gridview编辑时使用代码,

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gridView1= (GridView)sender;
       // Change the row state
        gridView1.Rows[e.NewEditIndex].RowState = DataControlRowState.Edit;
    
    }
    

    对于更新(这是示例代码)

     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
          GridView gv = (GridView)sender;
          GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
          TextBox TxtRejectReason= (TextBox)gvr.FindControl("TxtRejectReason");
          string s = TxtRejectReason.Text;
          GridView.EditIndex = -1;
          GridView.DataBind();
     }
    

    【讨论】:

    • thnks for his... 编辑选项正在运行 nw.. 但是当我单击更新时它的说法(索引超出范围。必须是非负数并且小于集合的大小。参数名称:index)
    • 我在项目模板中使用标签,在编辑项目模板编辑列中使用文本框..我应该在 nw 标签或文本框后面的代码中使用什么
    • 一旦我编辑和更新它说没有找到记录我使用了你给的 d 代码
    • 我不明白发生了什么...当我插入值并单击更新它的显示时,没有找到任何记录,任何人都可以说是这样
    猜你喜欢
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2020-01-13
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多