c#选择行datagridview

时间:2016-06-30 13:23:29

标签: c# asp.net .net

我有一个问题而且我不知道如何解决它,它让我完全疯狂!

我有一个填充了数据表的gridview,我希望能够点击一行来选择它。选中后,我希望它改变颜色,然后打开一个弹出窗口以仅显示该行。

我有一个代码来制作点击事件并改变颜色,但它不起作用。我没有任何错误,但当我点击一行时,我的gridview消失了,只有工具提示正常工作:

    <asp:GridView ID="gvData" runat="server" OnRowDataBound="gvData_RowDataBound" OnSelectedIndexChanged="gvData_SelectedIndexChanged" CssClass="mygrid" AutoGenerateColumns="false">

 <Columns>


                <asp:TemplateField HeaderText="Aggregation">
                    <ItemTemplate><%# Eval("Aggregation") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DerniereSolution">
                    <ItemTemplate><%# Eval("DerniereSolution") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DescriptionDemande">
                    <ItemTemplate><%# Eval("DescriptionDemande") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="NomContact">
                    <ItemTemplate><%# Eval("NomContact") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Numero">
                    <ItemTemplate><%# Eval("Numero") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SousRubrique">
                    <ItemTemplate><%# Eval("SousRubrique") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TitreDemande">
                    <ItemTemplate><%# Eval("TitreDemande") %></ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>


protected void Populate()
{
    string strSql = @"SELECT CAST(ID as VarChar(50)) ID, Aggregation,DateDerniereSolution, DateDescription, DerniereSolution, DescriptionDemande, FileDeTraitement, NomContact, Numero, SousRubrique, TitreDemande FROM DBTable";

    DataTable DTDeskCache = DataBaseCacheDigitalHepDeskConnection.SqlDataTable(strSql, "DataBaseName");

    gvData.DataSource = DTDeskCache;
    gvData.DataBind(); 
}

protected void btnSearch_Click(object sender, EventArgs e)
{
    Populate();
}

protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
           e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvData, "Select$" + e.Row.RowIndex);
           e.Row.ToolTip = "Click to select this row.";
     }
}

protected void gvData_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvData.Rows)
    {
        if (row.RowIndex == gvData.SelectedIndex)
        {
            row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            row.ToolTip = string.Empty;
        }
        else
        {
            row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
            row.ToolTip = "Click to select this row.";
        }
     }
 }

此代码无法执行任何操作,就像我没有编写任何有关选择行的代码一样。

所以我尝试过这样的事情:dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;但它说SelectionMode不存在,我试过.Selected = true,我得到了同样的消息,我试过:dtgView.Rows [i] .Cells [0] .Selected = true;我得到了同样的信息,我生气了,我不知道该怎么做才能使这项工作看起来很难,请有人帮助我

0 个答案:

没有答案