Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have display 3 column in datagridview named id,data1,data2. Now I have problem that i want to id column and then there is 1 link button in gridview.

When I click on that button at that time in "rowdeleting" event i want value of id from that row.

Kindly help me.

Thanks in advance.
Posted

The following code snippet will to hide the value associated with gridview column.
XML
<asp:GridView ID="gvData" runat="server"
                            AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
                            CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" Height="272px" Style="font-family: Consolas">
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <Columns>
                                <asp:TemplateField HeaderText="Data 1">
                                    <ItemTemplate>
                                        <asp:Label ID="lblId" runat="server" Text='<%# Bind("id") %>' Visible="false"></asp:Label>
                                        <asp:Label ID="lblData1" runat="server" Text='<%# Bind("data1") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Data 2">
                                    <ItemTemplate>
                                        <asp:Label ID="lblData2" runat="server" Text='<%# Bind("data2") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>


The id column is associated with non visible label. So you can access the data that is binded with this label.
 
Share this answer
 
Use this code

XML
<asp:GridView ID="GridView1" runat="server" CssClass="GridViewStyle"
        DataKeyNames="id" AutoGenerateColumns="False" OnRowDeleting="DeleteRecord"
               AllowPaging="True" AllowSorting="true" PageSize="15"
          OnPageIndexChanging="ChangePage" OnSorting="SortRecords">
            
           <Columns>
           <asp:BoundField DataField="id" HeaderText="S No" ReadOnly="True"  SortExpression="id" Visible="false" />
 
Share this answer
 
Comments
Herman<T>.Instance 7-Sep-11 6:37am    
DataKeyNames is indeed THE solution
Just give its width to 0%.

Hope this can help you.
 
Share this answer
 
Comments
uvp_07ce41 23-Apr-11 0:55am    
frnd still it shows tht column.

is there any another solution?
You can get the row by using the GridViewDeletedEventArgs.RowIndex and from the row access the cell.
C#
void GridView_RowDeleted(Object sender, GridViewDeletedEventArgs e)
{
    int id = gridView.Rows[e.RowIndex].Cells["id"].Text;
}
 
Share this answer
 
Comments
uvp_07ce41 23-Apr-11 0:45am    
but frnd i want to hide id columan and then also i want value of id when that row selected. in rowdeleting event
Tarakeshwar Reddy 23-Apr-11 0:56am    
You can follow what Prasanta_Prince said to hide your row.
uvp_07ce41 23-Apr-11 1:48am    
but its not working. When i make it width to 0 still it display that column.

Is there any another solution?
This is the code snippet, you can enhance and use as you require. Just handle the row-editing event in code-behind.

<pre lang="xml">asp:GridView ID="gvwTest" runat="server" CssClass="display" AutoGenerateColumns="False" DataKeyNames="id" OnRowEditing="gvwTest_RowEditing" Width="100%">
    <columns>
        <asp:boundfield datafield="code" headertext="Code" itemstyle-width="75px" xmlns:asp="#unknown" />
        <asp:boundfield datafield="name" headertext="Name" xmlns:asp="#unknown" />
        <asp:commandfield showeditbutton="True" itemstyle-width="50px" xmlns:asp="#unknown" />
    </columns

>
 
Share this answer
 
better solution for this is
i think

hiddenfield it will work fine
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900