1

While debugging I found that ItemID value is getting null. I am trying to get the list item's ID.

   ItemId = GridView1.DataKeys[e.RowIndex].Value.ToString();

Can anyone please tell me what could be the reason and how to solve this?

I have tried different things but nothing is working.

Is there anything wrong with this gridview?

<asp:GridView ID="GridView1" runat="server" OnRowEditing="Edit_row" OnRowCancelingEdit="cancel_Edit" OnRowDeleting="Delete_row" OnRowUpdating="Update_row" AutoGenerateColumns="true" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
               <Columns>
                   <asp:TemplateField HeaderText="Sr No.">
                       <ItemTemplate>
                           <%# Container.DataItemIndex+1 %>
                       </ItemTemplate>
                   </asp:TemplateField>

               </Columns>

                 <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#CCCC99" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FBFBF2" />
                <SortedAscendingHeaderStyle BackColor="#848384" />
                <SortedDescendingCellStyle BackColor="#EAEAD3" />
                <SortedDescendingHeaderStyle BackColor="#575357" />
            </asp:GridView>

1 Answer 1

1

I guess you will need set DataKeyNames property of gridview. I don't see that property set in your Gridview.

Hence ItemId = GridView1.DataKeys[e.RowIndex].Value.ToString(); returning null value.

For reference: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys(v=vs.110).aspx

https://msdn.microsoft.com/en-in/library/system.web.ui.webcontrols.gridview.datakeynames(v=vs.110).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.