开发者

Access Cell value in gridview

I thought this would be an easy one for me to figure out (or atl east find someone online who has) but I am running into issues.

I have the following code that creates my gridview:

    <asp:GridView runat="server" ID="ContactsGri开发者_运维问答d" AutoGenerateColumns="False" DataSourceID="LinqContact"
            CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="ContactsGridView_RowDeleting" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:BoundField DataField="IConact_ID" Visible="false" ReadOnly="true" />
        <asp:BoundField DataField="cFirstName" HeaderText="First Name" ReadOnly="True" />
        <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
    </Columns>
</asp:GridView>

<asp:LinqDataSource ID="LinqContact" runat="server" ContextTypeName="TIPS.App_Data.TIPSDataContext" onselecting="LinqContact_Selecting" >
</asp:LinqDataSource>

Now on the c# side, I want to be able to pull the value from the first column (which is hidden) and use it to delete that specific record (with the onroedeleting event), but all the ways I found to pull the value, all come up null, like what would happen if I didn't have a LinqDatasource.

I have tried (and a slew of other that really didn't seem to be right, so not listed):

ContactsGrid.SelectedRow.Cells[0].Text;
ContactsGrid.Columns[0];

Thanks for any help!

Edit:

Ok, so I found that you can't get the value of a column this is hidden when you hide it using the grid. I did find a work around. If you hide the column using css instead, you can still access the colum.

<style type="text/css">
.hiddencol
{
    display:none;
}

</style>

<asp:BoundField DataField="IContact_ID"  ReadOnly="true" itemstyle-cssclass="hiddencol" />

I don't think that this is the prefered .net way. I found a reference to something called datakeynames which seems to be the proper way. I am going to dig into those next.

Edit #2:

I see that Maras and myself both came up with a solution for the hidden field, but I think I found the best one (cause its simple and built in).

In the gridview tag you can set the datakeynames attribute to your column (like a primary key, which is what I was storing in my hidden column) you can also store multiple columns.

    <asp:GridView runat="server" ID="ContactsGrid" AutoGenerateColumns="False" DataSourceID="LinqContact" DataKeyNames="IContact_ID"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="ContactsGridView_RowDeleting" >

you can then reference it with:

ContactsGrid.DataKeys[e.RowIndex].Value;


Try this:

void ContactsGrid_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
    ContactsGrid.Rows[e.RowIndex].Cells[0];
}

See here for more details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting.aspx#Y200

.

Edit after author's comment:

If you set it visibility to 'false' then there is nothing your browser can send you back in postback. You can use hidden field like in the second example:

<asp:GridView runat="server" ID="gv" OnRowDeleting="gv_RowDeleting" AutoGenerateColumns="false" AutoGenerateDeleteButton="true">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="hf" runat="server" Value="<%# ((YourItemType)Container.DataItem).Id %>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    HiddenField hf = (HiddenField) gv.Rows[e.RowIndex].Cells[0].FindControl("hf");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜