加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用gridview中的OnSelectChange打印到标签

发布时间:2020-12-16 01:59:08 所属栏目:百科 来源:网络整理
导读:我在VS2012中使用asp.net和C#在我的数据网格上进行了On Select更改,但由于某种原因,所选行没有在我的标签中打印出来,我不是C#的专家,虽然我正在学习可以有人请告诉我是否我犯了一个错误. 谢谢 C#代码 public void Latest_DVD(){ { using (OleDbDataAdapter d
我在VS2012中使用asp.net和C#在我的数据网格上进行了On Select更改,但由于某种原因,所选行没有在我的标签中打印出来,我不是C#的专家,虽然我正在学习可以有人请告诉我是否我犯了一个错误.

谢谢

C#代码

public void Latest_DVD()
{
    {
        using (OleDbDataAdapter dataquer = new OleDbDataAdapter("SELECT Title,Category,Director,Stock,Year FROM DVD ",conn))
        {
            dataquer.Fill(dt);
        }
    }
    DG_Latest.ShowHeader = true;
    DG_Latest.DataSource = dt;
    DG_Latest.DataBind();
    conn.Close();
    conn.Dispose();
}

void Latest_DVD_SelectedIndexChanged(Object sender,EventArgs e)
{
    GridViewRow row = DG_Latest.SelectedRow;

    MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}

asp.net

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />             
       </asp:GridView>

解决方法

您需要正确连接SelectedIndexChangedEvent

试试这个:

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True"   
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" GridLines="Vertical" 
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />             
   </asp:GridView>

代码背后:

protected void Latest_DVD_SelectedIndexChanged(Object sender,EventArgs e)
{
GridViewRow row = DG_Latest.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读