从GridView访问绑定数据

时间:2014-09-01 03:55:33

标签: c# asp.net

我有一个asp.net Web项目,我想添加一个GridView,我已经完成了。我已经尝试将它绑定到ObjectDataSource并在Code后面,两者都可以正常工作。我绑定到一个表,其中有8列我要显示3并且在OnSelectedIndexChanged事件处理程序中我想检索绑定记录并填充详细控件但是当我查看行dataitem时它是null并且单元格只包含我选择在网格中显示的数据。我知道那里必须是一种方式,但我已经搜索了几个小时,我可能只是没有输入正确的搜索条件,但我很难过。

<div class="help-control-wrapper">
    <div class="help-control-header">
    </div>
    <div class="help-control-detail">
        <table id="Table1" class="t1" summary="" runat="server">
        </table>
    </div>
    <div class="help-control-grid">
        <asp:GridView runat="server" ID="GridView1" 
                    AutoGenerateColumns="false" 
                    HeaderStyle-BackColor="#DDEEFF"
                    OnRowDataBound="OnRowDataBound"
                    OnSelectedIndexChanged="OnSelectedIndexChanged">

                    <rowstyle backcolor="LightCyan"  
                        forecolor="DarkBlue"
                        font-italic="true"
                        height="16px"/> 

                    <alternatingrowstyle backcolor="PaleTurquoise"  
                        forecolor="DarkBlue"
                        font-italic="true"
                        height="16px"/>               
                <Columns>
                    <asp:BoundField DataField="CommentID" Visible="false" />
                    <asp:BoundField DataField="ParentName" HeaderText="Context" HeaderStyle-HorizontalAlign="Left"
                        ItemStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="CommentorName" HeaderText="Name" HeaderStyle-HorizontalAlign="Left"
                        ItemStyle-HorizontalAlign="Left" />
                    <asp:BoundField DataField="CommentorEmail" HeaderText="Email" HeaderStyle-HorizontalAlign="Left"
                        ItemStyle-HorizontalAlign="Left" />
            </Columns>
        </asp:GridView>
    </div>
</div>
<asp:ObjectDataSource ID="CommentDataSource" runat="server" SelectMethod="GetAllComments"
    TypeName="CommentService">
</asp:ObjectDataSource>

然后在

背后的代码中
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == GridView1.SelectedIndex)
        {
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");

            PopulateItem();
        }
        else
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
    }
}

0 个答案:

没有答案