0

您可以在下面找到我的网格的代码,到目前为止一切正常。现在我想在 RegionProjectname 上添加 Click 事件,这样当我单击 Region 项目名称行时,该列的 RegionProjectID 将存储在会话变量中,并且将根据所选行打开一个新网格。请编辑或指导我如何做到这一点。

我已经在网格中添加了 Select 行,它也可以正常工作,但我发表了评论。

 <asp:GridView ID="ResultGridView" runat="server" AutoGenerateColumns="False" ShowFooter="true"
    DataKeyNames="RegionProjectID" 
    AllowPaging="True" 
    CellPadding="3" 
    OnPageIndexChanging="ResultGridView_PageIndexChanging" 
    OnRowDeleting="ResultGridView_RowDeleting" 
    CssClass="mGrid"
    OnRowEditing="ResultGridView_RowEditing" OnRowUpdating="ResultGridView_RowUpdating" OnRowCancelingEdit="ResultGridView_RowCancelingEdit" PageSize="15" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" OnRowCommand="ResultGridView_RowCommand" >
        <Columns>
            <asp:BoundField DataField="RegionProjectID" HeaderText="Region ID" InsertVisible="False"
                ReadOnly="True" SortExpression="RegionProjectID" Visible="false" />
            <asp:TemplateField HeaderText="Region Name" SortExpression="RegionProjectName">
                <EditItemTemplate>
                    <asp:TextBox ID="txtRegion" Width="250px" runat="server" Text='<%# Bind("RegionProjectName") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID="txtRegion1" runat="server"  Width="250px"></asp:TextBox> 
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("RegionProjectName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Edit" ShowHeader="False"> 
            <EditItemTemplate> 
              <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
              <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
            </EditItemTemplate> 
            <FooterTemplate> 
              <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> 
            </FooterTemplate> 
            <ItemTemplate> 
              <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> 
            </ItemTemplate> 
            </asp:TemplateField> 
            <asp:CommandField HeaderText="Delete" ShowDeleteButton="True"  ShowHeader="True" /> 
           <%-- <asp:CommandField HeaderText="Select" ShowSelectButton="True"  ShowHeader="True" on/> --%>

        </Columns>
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <%--<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%>
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
private void FillVendorGrid()
    {
        dataTable = new DataTable();
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM RegionAndProjectInfo where natureofWorkID= " + ddlnatureOfWork.SelectedValue.ToString(); ;
        ad = new SqlDataAdapter(cmd);
        ad.Fill(dataTable);
        ResultGridView.DataSource = dataTable;
        ResultGridView.DataBind();
    }

    protected void ResultGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        ResultGridView.EditIndex = e.NewEditIndex;
        FillVendorGrid();
    }

    protected void ResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ResultGridView.PageIndex = e.NewPageIndex;
        FillVendorGrid();
    }

    protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        cmd.Connection = conn;
        cmd.CommandText = "DELETE FROM RegionAndProjectInfo WHERE RegionProjectID='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        FillVendorGrid();

    }

    protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtRegionname = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtRegion");
        TextBox txtNatureOFWork = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtNatureOFWork");
        cmd.Connection = conn;
        cmd.CommandText = "UPDATE RegionAndProjectInfo SET RegionProjectName ='" + txtRegionname.Text + "'   WHERE RegionProjectID='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
        conn.Open();
        cmd.ExecuteNonQuery();
        ResultGridView.EditIndex = -1;
        FillVendorGrid();
        conn.Close();

    }

    protected void ResultGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        ResultGridView.EditIndex = -1;
        FillVendorGrid();

    }

    protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {

            TextBox txtRegion1 = (TextBox)ResultGridView.FooterRow.FindControl("txtRegion1");
            TextBox txtNatureOFWork1 = (TextBox)ResultGridView.FooterRow.FindControl("txtNatureOFWork1");
            if (txtRegion1.Text != "")
            {
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO RegionAndProjectInfo(RegionProjectName, NatureOFWorkID ) Values('" + txtRegion1.Text + "', '" + ddlnatureOfWork.SelectedValue.ToString() + "')";
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            FillVendorGrid();
            conn.Close();
        }
    }

    protected void ddlnatureOfWork_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillVendorGrid();
    }
4

1 回答 1

0

您好,您可以尝试使用此代码

  1. 首先在您的列中添加 ButtonField

    <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
    
  2. 添加事件 GridView_RowCommand

    void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
            if(e.CommandName=="Add")
            {
               int index = Convert.ToInt32(e.CommandArgument);
    
               GridViewRow row = ContactsGridView.Rows[index];
    
    
               Session["YourKey"] = ...;
            } 
    
    }
    
于 2012-07-30T08:48:47.773 回答