Solved

gridview row id

Posted on 2010-09-02
5
409 Views
Last Modified: 2012-05-10
i need to get the gridview row id of a selected row. my issue is that I'm calling an object that I can't look into so I'm allowing the gridview to auto generate the columns. when the gridview is loaded i then need to be able to select a row and pass the rowid to a method. I have to click a button to load the data.
 I've already written the code to go after the data.

how do i code the selection of a row?
<asp:Button ID="btnListFiberTests" runat="server" 
                onclick="btnListFiberTests_Click" Text="List Fiber Tests" />

<asp:GridView ID="grdvwFiberTests" runat="server" CellPadding="4" 
                EnableModelValidation="True" ForeColor="#333333" GridLines="None" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            </asp:GridView>

-----------------

 protected void btnListFiberTests_Click(object sender, EventArgs e)
        {
            //get all rows of data
            proxy = new FiberMossClient.FiberServiceReference.FiberServiceClient();
            fibertest = proxy.ListFiberTests();
            
            grdvwFiberTests.AutoGenerateColumns = true;
            grdvwFiberTests.DataSource = fibertest;
            grdvwFiberTests.DataBind();
        }

Open in new window

0
Comment
Question by:vicomin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33590206

Yiou can use GridView.SelectedRow property to get the current selected row...

=> http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow.aspx
0
 

Author Comment

by:vicomin
ID: 33590224
modified the gridview as such - object browser helped
<asp:GridView ID="grdvwFiberTests" runat="server" CellPadding="4" 
                EnableModelValidation="True" ForeColor="#333333" GridLines="None" 
                AutoGenerateColumns="False" DataKeyNames="FiberTestId" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="FiberTestId" HeaderText="Fiber Test Id" 
                        SortExpression="FiberTestId" />
                    <asp:BoundField DataField="WorkOrder" HeaderText="Work Order" 
                        SortExpression="WorkOrder" />
                    <asp:BoundField DataField="SerialNumber" HeaderText="Serial Number" 
                        SortExpression="SerialNumber" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            </asp:GridView>

Open in new window

0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33590266

protected void button1_Click(object sender, EventArgs e)
{

   GridViewRow row = grdvwFiberTests.SelectedRow;

   // now to get the first col value

   string firstColValue = row.Cells[0].Text;

 // OR if you have set DataKeyNames property of the gridview to 'ID' column,
// SelectedValue property will give you the ID value directly
// DataKeyNames  => http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx

   string selectedId = grdvwFiberTests.SelectedValue;


}
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33590388

 You can use 'CommandField' column to select a row = > http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandfield.aspx
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33590437

Below code shows adding a commandfield with Selectbutton...when user clicks the link on the row...that row will be selected, then GridView.SelectedRow or GridView.SelectedValue can be used as I mentioned above...
...
<Columns>
                    <asp:BoundField DataField="FiberTestId" HeaderText="Fiber Test Id" 
                        SortExpression="FiberTestId" />
                    <asp:BoundField DataField="WorkOrder" HeaderText="Work Order" 
                        SortExpression="WorkOrder" />
                    <asp:BoundField DataField="SerialNumber" HeaderText="Serial Number" 
                        SortExpression="SerialNumber" />
<asp:commandfield ShowSelectButton="true"
            headertext="Select"/>
</Columns>
....

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question