How to Obtain Gridview Row Item from OnClick Event

rito1
rito1 used Ask the Experts™
on
Hi All,

I have the attached Gridview and OnClick event.

Please could someone fill in the blanks and show me how to obtain the value of the BoundField ImageID and also the Text from the Template field control txtAltText?

Many thanks,


Rit
<asp:GridView ID="GridViewPhotos" runat="server" CssClass="customer-table" DataSourceID="SqlDataSourcePhotos"
                    AutoGenerateColumns="False" DataKeyNames="ImageID">
                    <Columns>
                    <asp:BoundField DataField="ImageID" Visible="true" />
                        <asp:TemplateField HeaderText="Caption" SortExpression="AltText">
                            <ItemTemplate>
                                <asp:TextBox ID="txtAltText" runat="server" Text='<%# Bind("AltText") %>' CssClass="texfield-medium"></asp:TextBox>
                            </ItemTemplate>
                            <EditItemTemplate>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>



protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gvr in GridViewPhotos.Rows)
        {
            if (gvr.RowType == DataControlRowType.DataRow)
            {
                //**********
            }

            // Call database update method with params
        }
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Please see an example of how to get the selected row...


~Ajitha

aspx file...

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:GridView ID="GridViewPhotos" runat="server"
            AutoGenerateColumns="False" DataKeyNames="CourseID" AutoGenerateSelectButton="true" OnSelectedIndexChanged="GridViewPhotos_RowSelected">
            
            <Columns>
            
            <asp:BoundField DataField="CourseID" Visible="true"/>
            <asp:TemplateField HeaderText="Caption" SortExpression="AltText">
                <ItemTemplate>
                    <asp:TextBox ID="txtAltText" runat="server" Text='<%# DataBinder.Eval (Container.DataItem, "Title")  %>'></asp:TextBox>
                </ItemTemplate>
                <EditItemTemplate>
                </EditItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView> 

.cs code.....

        protected void GridViewPhotos_RowSelected(object sender, EventArgs e)
        {
            Label1.Text = "Selected Row: " + GridViewPhotos.SelectedRow.Cells[1].Text; ;
        }

Open in new window

Author

Commented:
Hi ajitha75,

I don't think this is quite what I am looking for. Ultimately what I need to do is loop through all Gridview rows and perform a database update against each row. Though its obtaining the values from rows that I am struggling with.

Rit
Commented:
you can loop through the rows and for each row select .Cell[0], .Cell[1] for first, second column. The text value of that will give you the values in the grid.

~Ajitha
Commented:
Hey,

Here's a code example
foreach (GridViewRow gvr in GridViewPhotos.Rows)
{
    if (gvr.RowType == DataControlRowType.DataRow)
    {
       string firstColValue=gvr.Cells[0].Text;
       string secondColValue=gvr.Cells[1].Text;

       //etc...
    }

    // Call database update method with params
}

Open in new window

Author

Commented:
Thanks Both

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial