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