gridview编辑和更新

时间:2013-07-26 06:45:11

标签: c# asp.net gridview

我有一个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 Column。

cs page

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中的编辑按钮,它就不显示记录

请帮帮我

2 个答案:

答案 0 :(得分:2)

尝试以下代码:

<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();
  }

答案 1 :(得分:1)

在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();
 }