0

I was wondering how to retrieve a specific data from gridview. My database consists of columns like id, type and name. Lets say the contents of my gridview is like this example below:

Id         Type         Name

1         Guitar         Ibanez

2         Guitar         Gibson

What i want to happen is to get the value under Name, Lets say "Ibanez". I was able to retrieve the value for Id using datakeys but I can't figure out how to get the value of Name in Gridview. I included code below to better understand what i mean.

protected void GuitarBrandsGridViewBtn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow gridrow = btn.NamingContainer as GridViewRow;
    int id = Convert.ToInt32(GuitarBrandsGridView.DataKeys[gridrow.RowIndex].Value.ToString());
    con.Open();
    cmd.CommandText = "DELETE FROM [guitarBrands] WHERE id=" + id;
    cmd.Connection = con;
    int a = cmd.ExecuteNonQuery();
    con.Close();
    if (a > 0)
    {
        bindgridviewguitarbrands();
    }
    System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItemsIbanezDetails" + id + ".aspx");
    System.IO.File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItemsIbanezDetails" + id + ".aspx.cs");

}
1
  • r u using any collections Commented Feb 16, 2017 at 5:15

2 Answers 2

1

You can access specific columns like this

GuitarBrandsGridView.Rows[gridrow.RowIndex].Cells[1].Text;
Sign up to request clarification or add additional context in comments.

1 Comment

This is the exact answer im looking for.
0

you can use template field in the grid view instead of bound field like this :

<asp:TemplateField HeaderText="Name">
     <ItemTemplate>
          <asp:Label ID="lblName" runat="server" Text='<%# Eval("ColumnNameOfYourDatasourceForName") %>' />
     </ItemTemplate>
</asp:TemplateField>

and then get the label and its text in code behind from following code :

protected void GuitarBrandsGridViewBtn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow gridrow = btn.NamingContainer as GridViewRow;

    Label lblName = gridrow.FindControl("lblName");
    string name = lblName.Text;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.