Af'faq
Af'faq

Reputation: 527

Selecting Particular Column in a Gridview

I have a gridview in which there are multiple records i am showing. On a page load event, i am retrieving data from database and populate the gridview. I have another column for edit and delete which are Imagebuttons. Now i want when a user click on edit button, then on the bases of Id (of that row upon a user clicked),he/she redirects to another webform with the selected Id and then that person records loads from database and populate the corresponding textboxes. I am not updating records within Gridview. Here is the image for clear understandings enter image description here

Here is my .aspx code

<asp:GridView ID="dgvEmployeesInformation" runat="server" CssClass=" table table-bordered table-hover table-responsive" AutoGenerateColumns="false" OnRowCommand="dgvEmployeesInformation_RowCommand">
                <%--1st Column--%>
                <Columns>
                    <asp:BoundField  HeaderText="ID" DataField="Id" ShowHeader="false" Visible="false" ControlStyle-BackColor="#0066ff"/>
                    <asp:BoundField  HeaderText="Name" DataField="Name"/>
                    <asp:BoundField  HeaderText="Employee No" DataField="EmployeeNo"/>
                    <asp:BoundField  HeaderText="Father Name" DataField="FatherName"/>
                    <asp:BoundField  HeaderText="CNIC" DataField="CNIC"/>
                    <asp:BoundField  HeaderText="Contact No" DataField="ContactNo"/>
                    <asp:BoundField  HeaderText="City" DataField="City"/>
                    <asp:BoundField  HeaderText="Post" DataField="RecommendedPost"/>
                    <asp:BoundField  HeaderText="Status" DataField="Status"/>
                    <asp:BoundField  HeaderText="Degree Status" DataField="DegreeStatus"/>
                    <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                            <asp:ImageButton ImageUrl="~/Images/edit.png" CommandName="Edit" Text='<%# Eval("ID") %>' ToolTip="Edit" Width="20px" Height="20px" runat="server"/>
                            <asp:ImageButton ImageUrl="~/Images/delete.png" CommandName="Delete" Text='<%# Eval("ID") %>' ToolTip="Delete" Width="20px" Height="20px" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

Here is .aspx.cs Code

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dgvEmployeesInformation.DataSource = GetData();
            dgvEmployeesInformation.DataBind();
        }
    }

    private DataSet GetData()
    {
        DataSet DS = new DataSet();
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {

            SqlDataAdapter ad = new SqlDataAdapter("spSelectEmployeeSpecificRecord", con);
            ad.SelectCommand.CommandType = CommandType.StoredProcedure;
            ad.Fill(DS);
        }
        return DS;
    }
protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Edit"))
        {

        }
    }

I have no idea how to get ID of the selected row and send it to another page. Please Help

Upvotes: 2

Views: 3588

Answers (2)

Aristos
Aristos

Reputation: 66641

the e.CommandArgument contains the index you have click on it. and the dgvEmployeesInformation.SelectedValue contains the Id that you have set on DataKeyNames.

Set on DataKeyNames the database field you have as id - in your case DataKeyNames="EmployeeNo" and the code can be as

protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        int SelectedIndex;
        if (int.TryParse(e.CommandArgument.ToString(), out SelectedIndex))
        {
            // make it selected after the click
            dgvEmployeesInformation.SelectedIndex = SelectedIndex;

            // now the SelectedValue contains the id
            Edit(dgvEmployeesInformation.SelectedValue);
        }
    }
}

Upvotes: 1

Amine
Amine

Reputation: 142

Try this

void yourGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Edit")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked 
// by the user from the Rows collection.
GridViewRow row = ContactsGridView.Rows[index];
var yourID = row.Cells[0].Text;
//....
}
}

Grid view learn.microsoft.com

Upvotes: 0

Related Questions