Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
How to Store SelectedRow.Cell[0] value in a session when CommandField is "Select" in a GridView. ? What the changes i will do in my code so that when a Select Command executes on my grid (By Clicking on Select Link of my grid), It will Store the NatureOfWorkID of that row in the session variable. Below is my code. Please guide me that what changes required in the below code so that when ever some one will click on Select, the natureofworkID of that row will be stored in a Session and i will bind another grid from the session variable as per my requirement.
<asp:GridView ID="GVNatureOFWork" runat="server" AllowPaging="true" ShowFooter="true" PageSize="10" AutoGenerateColumns="false"
    DataKeyNames="NatureOfWorkID"
     OnPageIndexChanging="GVNatureOFWork_PageIndexChanging" 
     onrowcancelingedit="GVNatureOFWork_RowCancelingEdit" 
     onrowcommand="GVNatureOFWork_RowCommand" onrowdeleting="GVNatureOFWork_RowDeleting" 
     onrowediting="GVNatureOFWork_RowEditing" onrowupdating="GVNatureOFWork_RowUpdating" 
     OnSelectedIndexChanged="GVNatureOFWork_SelectedIndexChanged" OnRowDataBound="GVNatureOFWork_RowDataBound"
     HeaderStyle-BackColor="BlueViolet" 
     HeaderStyle-ForeColor="White" BackColor="White">
        <Columns>
            <asp:TemplateField HeaderText="NatureOfWorkID">
                <ItemTemplate>
                    <asp:Label ID="lblNautureOfWorkID" runat="server" Text='<%#Eval("NatureOfWorkID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField HeaderText="Nature of Work Name">
                <ItemTemplate>
                    <asp:Label ID="lblNatureOfWorkName" runat="server" Text='<%#Eval("NatureOfWorkName") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtNatureOfWorkName" runat="server" Text='<%#Eval("NatureOfWorkName") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtAddNatureOfWorkName" runat="server"  />
                </FooterTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField HeaderText="Enable/Disable">
                <ItemTemplate>
                    <asp:Label ID="lblNOWStatus" runat="server" Text='<%# (int)Eval("IsNOWEnabled") == 1 ? "Enabled" : "Disabled" %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:RadioButtonList ID="radiolistStatus" runat="server">
                    <asp:ListItem Text="Enable" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Disable" Value="0"></asp:ListItem>
                    </asp:RadioButtonList>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:RadioButtonList ID="radiolistStatusAdd" runat="server">
                    <asp:ListItem Text="Enable" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Disable" Value="0"></asp:ListItem>
                    </asp:RadioButtonList>
                </FooterTemplate>
                </asp:TemplateField>
 
            <asp:TemplateField  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 ShowSelectButton="True" ItemStyle-CssClass="HiddenColumn" HeaderStyle-CssClass="HiddenColumn"/> 
      </Columns>
  </asp:GridView><br /><br /><br /><br />
 
<asp:GridView ID="GVRegionProject" runat="server" AllowPaging="true" ShowFooter="true" PageSize="10" AutoGenerateColumns="false"
     OnPageIndexChanging="GVRegionProject_PageIndexChanging" 
     onrowcancelingedit="GVRegionProject_RowCancelingEdit" 
     onrowcommand="GVRegionProject_RowCommand" onrowdeleting="GVRegionProject_RowDeleting" 
     onrowediting="GVRegionProject_RowEditing" onrowupdating="GVRegionProject_RowUpdating" 
     OnSelectedIndexChanged="GVRegionProject_SelectedIndexChanged"
     HeaderStyle-BackColor="BlueViolet" 
     HeaderStyle-ForeColor="White" BackColor="White">
     <Columns>
     <asp:TemplateField HeaderText="Region/Project ID">
                <ItemTemplate>
                    <asp:Label ID="lblRegionProjectID" runat="server" Text='<%#Eval("RegionProjectID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField HeaderText="Region/Project Name">
                <ItemTemplate>
                    <asp:Label ID="lblRegionProjectName" runat="server" Text='<%#Eval("RegionProjectName") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtRegionProjectName" runat="server" Text='<%#Eval("RegionProjectName") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtAddRegionProjectName" runat="server"  />
                </FooterTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField HeaderText="Enable/Disable">
                <ItemTemplate>
                    <asp:Label ID="lblRPStatus" runat="server" Text='<%# (int)Eval("IsRegionProjectEnabled") == 1 ? "Enabled" : "Disabled" %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:RadioButtonList ID="radiolistStatus" runat="server">
                    <asp:ListItem Text="Enable" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Disable" Value="0"></asp:ListItem>
                    </asp:RadioButtonList>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:RadioButtonList ID="radiolistStatusAdd" runat="server">
                    <asp:ListItem Text="Enable" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Disable" Value="0"></asp:ListItem>
                    </asp:RadioButtonList>
                </FooterTemplate>
                </asp:TemplateField>
 
            <asp:TemplateField  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="County" ShowHeader="false" ShowSelectButton="True"/>
 

 </Columns>
 </asp:GridView>
Code Behind is :-
private void BindGrid()
    {
        dataTable = new DataTable();
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM natureOfWork";
        ad = new SqlDataAdapter(cmd);
        ad.Fill(dataTable);
        GVNatureOFWork.DataSource = dataTable;
        GVNatureOFWork.DataBind();
    }
 
    protected void GVNatureOFWork_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GVNatureOFWork.PageIndex = e.NewPageIndex;
        BindGrid();
    }
 
    protected void GVNatureOFWork_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GVNatureOFWork.EditIndex = -1;
        BindGrid();
 
    }
 
    protected void GVNatureOFWork_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
 
            TextBox txtAddNatureOfWorkName = (TextBox)GVNatureOFWork.FooterRow.FindControl("txtAddNatureOfWorkName");
            RadioButtonList radiolistStatusAdd = (RadioButtonList)GVNatureOFWork.FooterRow.FindControl("radiolistStatusAdd");
            if ((txtAddNatureOfWorkName.Text != "")&&(radiolistStatusAdd.SelectedValue != string.Empty))
             {
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO NatureOFWork(NatureOFWorkname, isNOWEnabled ) Values('" + txtAddNatureOfWorkName.Text + "', '" + radiolistStatusAdd.SelectedValue.ToString() + "')";
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            BindGrid();
            conn.Close();
        }
 
        if (e.CommandName.Equals("Select"))
        {
 

        }
    }
 
    protected void GVNatureOFWork_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblNautureOfWorkID = (Label)GVNatureOFWork.Rows[e.RowIndex].FindControl("lblNautureOfWorkID");
        cmd.Connection = conn;
        cmd.CommandText = "DELETE FROM NatureOFWork WHERE NatureOfWorkID='" + lblNautureOfWorkID.Text.ToString() + "'";
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        BindGrid();
    }
 
    protected void GVNatureOFWork_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GVNatureOFWork.EditIndex = e.NewEditIndex;            
        BindGrid();
 
    }
 
    protected void GVNatureOFWork_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 
        Label lblNautureOfWorkID = (Label)GVNatureOFWork.Rows[e.RowIndex].FindControl("lblNautureOfWorkID");
        TextBox txtNatureOfWorkName = (TextBox)GVNatureOFWork.Rows[e.RowIndex].FindControl("txtNatureOfWorkName");
        RadioButtonList radiolistStatus = (RadioButtonList)GVNatureOFWork.Rows[e.RowIndex].FindControl("radiolistStatus");
        string isenabled = radiolistStatus.SelectedValue.ToString();
        cmd.Connection = conn;
        cmd.CommandText = "UPDATE NatureOfWork SET NatureOfWorkname ='" + txtNatureOfWorkName.Text + "',IsNowEnabled=" + isenabled.ToString() + "  WHERE NatureOFWorkID=" + lblNautureOfWorkID.Text.ToString();
        conn.Open();
        cmd.ExecuteNonQuery();
        GVNatureOFWork.EditIndex = -1;
        BindGrid();
        conn.Close();
    }
 
    protected void GVNatureOFWork_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblNautureOfWorkID = (Label)e.Row.FindControl("lblNautureOfWorkID");
            Label lblNatureOfWorkName = (Label)e.Row.FindControl("lblNatureOfWorkName");
            TextBox txtNatureOfWorkName = (TextBox)e.Row.FindControl("txtNatureOfWorkName");
            TextBox txtAddNatureOfWorkName = (TextBox)e.Row.FindControl("txtAddNatureOfWorkName");
            RadioButtonList radiolistStatusAdd = (RadioButtonList)e.Row.FindControl("radiolistStatusAdd");
            RadioButtonList radiolistStatus = (RadioButtonList)e.Row.FindControl("radiolistStatus");
            Label lblNOWStatus = (Label)e.Row.FindControl("lblNOWStatus");
 

        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
        }
    }
 
    protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
    {
        string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
        BindRegionProjectInfoGrid();
    }
Posted 10-Oct-12 21:26pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi friend,
you have written the code as..
protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
    {
        string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
        BindRegionProjectInfoGrid();
    }
in this method, jst write
session["value"]=natureOfWorkID;
then u can use session["value"] anyware in application...
  Permalink  
v2
Comments
Dharmenrda Kumar Singh at 12-Oct-12 2:02am
   
Here only i m getting null reference exception
protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
{
string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
BindRegionProjectInfoGrid();
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this
protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
    {
        string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
        session["natureofworkID"]=natureOfWorkID.ToString();
        BindRegionProjectInfoGrid();
    }
 
Thanks
  Permalink  
Comments
Dharmenrda Kumar Singh at 12-Oct-12 2:03am
   
Here only i m getting null reference exception
protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
{
string natureOfWorkID=GVNatureOFWork.SelectedRow.Cells[0].Text;
BindRegionProjectInfoGrid();
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi, i didnt see ur query properly
 

there is an event called 'rowcommand'..
 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{ // Retrieve the row index stored in the // CommandArgument property.

int index = Convert.ToInt32(e.CommandArgument);// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GVRegionProject.Rows[index];// Add code here to add the item to the shopping cart.
}
}

 
****************
 
and u should modify the tag as ....
 
<asp:linkbutton id="AddButton" runat="server" xmlns:asp="#unknown">
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Update" />

 
it may work, try it...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 protected void GVNatureOFWork_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = GVNatureOFWork.SelectedIndex;
            string natureofwrkid = GVNatureOFWork.DataKeys[index].Value.ToString();
            Session["NOWID"] = natureofwrkid;
            lblDisplayNOWID.Text = natureofwrkid;
            BindRegionProjectInfoGrid();
 
        }
 
By adding above line, i can store now the selected row Cell[0] value. Thanks to every one.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100