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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

(2.0) get GridView item 1

Status
Not open for further replies.

TipGiver

Programmer
Sep 1, 2005
1,863
Hi,
I am working on this since yesterday, and i cannot seem to get it working.

I have a sqlDatasource and a GridView bound to it. I also have a button appended at the end. In the RowCommand event handler i may know what was the rowIndex of the button that i clicked on (e.CommandArgument).
How can i get a single cell ? I am trying something like:

me.gridview1.rows(e.commandargument).columns("id")
The red do no exist.

The ID is the name a a hidden column in the gridview, and it is the primary key.
I have also added the ID to the gridview's datakeynames collection.

Any ideas?
Thanks
 
Can you clarify what you are trying to do? You have a button on each row of the gridview, correct? Do you have only one button, or more than one button?
 
Actually i have 2 buttons at the end of each row. They have a different CommandName. What i want is to get the ID so i can run an sql query in the RowCommand event. The query is: .... where id = @id.
 
ca8msm,

it is very simple one. I dragged a table from the server explorer onto the web form. Then i removed some columns that i didn't need to show, and modified the selectcommand of the created datasource. I removed the fields that i deleted from the gridview.

The query i want to run is an update. Do you think i should write the updateCommand in the datasource?
 
Some letters will not be appeared properly.

Also:
- The GridView is PendingGridView
- The Datasource is PendingSqlDS
- The buttons are bold and are image buttons

Code:
    <asp:GridView ID="PendingGridView" runat="server" AutoGenerateColumns="False" CellPadding="4"
        DataSourceID="PendingSqlDS"
        ForeColor="#333333" GridLines="Vertical" AllowPaging="True" Caption="<h1>??????????? ?? ???????????</h1><h5>????? ???? ??? 'v' ??? ?????????? ??? ????, ?????? ??? 'x' ??? ????????.<br><i>?? ????? ????? ????? ?? ????? ?????????.</h5></i>" CaptionAlign="Left" PageSize="5" DataKeyNames="OrderID">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:BoundField DataField="OrderID" ShowHeader="False" SortExpression="OrderID" Visible="False" />
            <asp:BoundField DataField="OrderDate" HeaderText="?????????? ???????????" SortExpression="OrderDate" >
                <FooterStyle HorizontalAlign="Left" />
                <HeaderStyle HorizontalAlign="Left" Width="200px" />
            </asp:BoundField>
            <asp:CheckBoxField DataField="Drilling" HeaderText="Drill" SortExpression="Drilling" >
                <ItemStyle Font-Size="XX-Small" HorizontalAlign="Center" />
                <FooterStyle HorizontalAlign="Center" />
                <HeaderStyle Width="40px" />
            </asp:CheckBoxField>
            <asp:CheckBoxField DataField="Silkscreen" HeaderText="Silk" SortExpression="Silkscreen" >
                <FooterStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle Width="40px" />
            </asp:CheckBoxField>
            <asp:CheckBoxField DataField="TinPlatting" HeaderText="Tin" SortExpression="TinPlatting" >
                <FooterStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle Width="40px" />
            </asp:CheckBoxField>
            <asp:BoundField DataField="Quantity" HeaderText="????????" SortExpression="Quantity" >
                <FooterStyle HorizontalAlign="Left" />
            </asp:BoundField>
            <asp:BoundField DataField="Price" HeaderText="????" SortExpression="Price" >
                <FooterStyle HorizontalAlign="Left" />
                <HeaderStyle Width="80px" />
            </asp:BoundField>
            [b]<asp:ButtonField[/b] ButtonType="Image" HeaderText="???????" ImageUrl="~/Images/yesno/yes.bmp" CommandName="yes" >
                <FooterStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle Width="70px" />
            </asp:ButtonField>
            [b]<asp:ButtonField[/b] ButtonType="Image" HeaderText="????????" ImageUrl="~/Images/yesno/no.bmp" CommandName="no" >
                <FooterStyle HorizontalAlign="Center" />
                <ItemStyle HorizontalAlign="Center" />
                <HeaderStyle Width="70px" />
            </asp:ButtonField>
        </Columns>
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <EditRowStyle BackColor="#999999" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>
 
OK, the easiest method is probably to drop the ButtonField columns and add TemplateField ones instead. e.g.
Code:
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button id="deleteme" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "OrderID") %>' CommandName="Yes" Text="Delete" />
            </ItemTemplate>
        </asp:TemplateField>
You can see that we set the CommandName to "Yes" and the CommandArgument will be set at runtime to the relevant OrderID. This will be accessible via the e.CommandArgument property in the RowwCommand event.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Very interesting.
I will try it right now and let you know

Your star :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top