Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Row in Gridview

Status
Not open for further replies.

tmilluah4

Programmer
Jan 16, 2007
8
US
I have a gridview where I want to select a row and do a postback...

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("onclick", "__doPostBack('ctl00$ContentPlaceHolder1$PharmacyGrid','Select$" + e.Row.RowIndex.ToString() + "')");
e.Row.Attributes.Add("style", "cursor:hand;");
e.Row.Attributes.Add("onmouseover", "this.originalcolor=this.style.backgroundColor;" + " this.style.backgroundColor='Yellow';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalcolor;");
}


This code selects a row and does a postback....
My question is how do I get the selected row values back out so I can insert them into another table????/
 
It depends what information is in the GridView and how it is stored. Depending on this data, one possibility would be to use the the GridView's "SelectedRow.Cells(0).Text" property where zero is the relevant Cell.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Here is what I have...

<asp:Gridview Visible="true" ID="PharmacyGrid" SelectedIndex="0" RowStyle-Font-Size="X-Small" EnableViewState="true" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="WhiteSmoke" runat="server" Width="750px" OnRowDataBound="PharmacyGrid_RowDataBound"
OnSelectedIndexChanged="PharmacyGrid_SelectedIndexChanged" OnRowCreated="GridView1_RowCreated" >

<HeaderStyle CssClass="searchHeader" BackColor="#666666" Height="20px" font-bold="true" font-name="Arial"
ForeColor="#ffffff" font-size="xx-small" width="100%" HorizontalAlign="left" ></HeaderStyle>

<Columns>
<asp:TemplateField HeaderText="Pharmacy" SortExpression="StoreName">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["StoreName"] %>'
ID="SearchDGLblName" ></asp:label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Street" SortExpression="AddressLine1">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["AddressLine1"] %>'
ID="SearchDGLblAddress">
</asp:label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="City" SortExpression="City">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["City"] %>'
ID="SearchDGLblCity" >
</asp:label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="State" SortExpression="State">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["State"] %>' ID="SearchDGLblState">
</asp:label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Zip" SortExpression="Zip">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["Zip"] %>' ID="SearchDGLblZip">
</asp:label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Phone" SortExpression="PhonePrimary">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["PhonePrimary"] %>' ID="SearchDGLblPhone" >
</asp:label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Fax" SortExpression="Fax">
<ItemTemplate>
<asp:label font-name="Arial" font-size="xx-small" font-bold="true" runat="server" width="100%"
Text='<%# ((System.Data.DataRowView)Container.DataItem)["Fax"] %>' ID="SearchDGLblFax">
</asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:Gridview>




*********************************************
This is the method that I want to insert the selected values from the gridview and insert them into this table....
*********************************************

protected void AddToPharmFavs(object sender, System.EventArgs e)
{
Button clickedButton = (Button)sender;

if (clickedButton.ID == "AddPharmFavsSearchBtn")
{
if (itemcellPharm == null || itemcellPharm == null && itemcellPhone == null && itemcellFax == null
&& itemcellStreet == null && itemcellCity == null && itemcellState == null && itemcellZip == null)
{}
else
{
AddPharmFavsSearchBtn.Attributes.Add("OnClientClick", "alert('Selected Pharmacy has been added to Pharmacy Favorites');");
digiChart.DAL.Pharmacy AddPharmacy = new digiChart.DAL.Pharmacy();

bool Add = AddPharmacy.Insert(
this.PracticeID,
itemcellPharm,
itemcellPhone,
itemcellFax,
itemcellStreet,
itemcellCity,
itemcellState,
itemcellZip);
}
}
}
 
In that case, I'd use the FindControl method of the SelectedRow to find each control you've set up in the TemplateColumns. Once you've got a reference to each control, you can just get the data from its relevant properties (i.e. Text property for a Label control).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I tried adding...

string test = ((Label)PharmacyGrid.SelectedRow.FindControl("SearchDGLblName")).Text;

but it always is the first item in the gridview and not the one I selected...

Is my RowCreated method correct? Shouldn't I be able to access the values of the row clicked after it has done the postback by adding the above code in the AddtoPharmFavs method?
 
When I debugged I found that the rowindex is always -1 in this piece of code so it is actually never getting the correct row...any ideas why?

e.Row.Attributes.Add("onclick", "__doPostBack('ctl00$ContentPlaceHolder1$PharmacyGrid','Select$" + e.Row.RowIndex.ToString() + "')");
 
Try using the RowDataBound event instead


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top