Question

i am working on visual stdio 2008 and my database is in sql server 2005 
MY table has three columns 
1. SenderName 
2. RecieverName
3. Message

i have displayed this table in GridView and add a button named as Reply 
so my grid view look's some what like this 

SenderName|RecieverName| MessAge|REPLY BUTTON

now this what i want to do 
when Button is Clicked in My gridView i need to get data of that specific row 
i.e Sender's NAme so that i can Reply him/her ?

can any one help????
Was it helpful?

Solution

Here's a sample:

Markup:

<asp:GridView 
    runat="server" 
    ID="gvEmails" 
    OnSelectedIndexChanged="gvEmails_SelectedIndexChanged">            
    <Columns>
        <asp:ButtonField CommandName="Select" ButtonType="Button" Text="Send" />
    </Columns>
</asp:GridView>

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("SenderName");
    dt.Columns.Add("ReceiverName");
    dt.Columns.Add("Message");

    DataRow dr;

    dr = dt.NewRow();
    dr["SenderName"] = "John Doe";
    dr["ReceiverName"] = "Jane Doe";
    dr["Message"] = "Hi, Jane.";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["SenderName"] = "Michelle Smith";
    dr["ReceiverName"] = "Mike Smith";
    dr["Message"] = "Yo, Mike.";
    dt.Rows.Add(dr);

    gvEmails.DataSource = dt;
    gvEmails.DataBind();
}

protected void gvEmails_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = gvEmails.SelectedRow;

    Response.Write("Send email to " + row.Cells[1].Text);
}

OTHER TIPS

there is a selected index changed function in the properties.

Captuer the selected index and get the cell value of that selected index.

Then continue whtever u want.

There are many different ways of doing this. The easiest, if you only need a single value, would be to bind the value to the CommandArgument of your Reply button. Then add an OnClick handler to your button. Then in the OnClick method you can get the name from the CommandArgument.

If you need more than a single value from the row, you will need to do a little more work. You can setup an event handler on the GridView to capture the event of the index changing. This will provide some event arguments that has a NewSelectedIndex. That will tell you what row was selected. Depending on how your data is bound to your GridView, you can access the data again to get the values you need, or you can set the columns to be a DataKey in the GridView and access them that way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top