如何在没有选择按钮的情况下选择和取消选择GridView中的行

本文关键字:选择 取消 GridView 情况下 有选择 按钮 | 更新日期: 2023-09-27 18:21:52

我正在开发一个Master-Details GridView&细节视图。一切都很好,但我希望它删除GridView中每行中的选择按钮,并使用户能够通过单击行中的任何位置或单击行中任何单元格来选择行。除此之外,我希望在他选择行时显示每行的详细信息,并在他再次单击同一行时隐藏详细信息那么怎么做呢

ASP.NET代码:

<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0" 
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" />
                        <Columns>
                            <asp:CommandField ShowSelectButton="true" />
                            <asp:BoundField DataField="QuestionID" HeaderText="Question" />
                            <asp:BoundField DataField="UserAnswer" HeaderText="Your Answer" />
                            <asp:BoundField DataField="Result" HeaderText="Result" />
                        </Columns>
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="boldtext" />
                        <EditRowStyle BackColor="#999999" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>

代码隐藏代码:

public partial class Results : System.Web.UI.Page
{
    bool bShowDetailsView;
    protected void Page_Load(object sender, EventArgs e)
    {
       bShowDetailsView = false;
        ArrayList al = (ArrayList)Session["AnswerList"];
        if (al == null)
        {
            Response.Redirect("default.aspx");
        }
        resultGrid.DataSource = al;
        resultGrid.DataBind();
        // Save the results into the database.
        if (IsPostBack == false)
        {
            // Calculate score
            double questions = al.Count;
            double correct = 0.0;

            for (int i = 0; i < al.Count; i++)
            {
                Answer a = (Answer)al[i];
                if (a.Result == Answer.ResultValue.Correct)
                    correct++;
            }
            double score = (correct / questions) * 100;
            string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO''", "");
            SqlDataSource userQuizDataSource = new SqlDataSource();
            userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
            userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (@QuizID, @DateTimeComplete, @Score, @Username)";
            userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
            userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
            // "N4" is for displaying four decimal places, regardless of what the value is 
            userQuizDataSource.InsertParameters.Add("Score", score.ToString("N4"));
            userQuizDataSource.InsertParameters.Add("Username", username);
            int rowsAffected = userQuizDataSource.Insert();
            if (rowsAffected == 0)
            {
                // Let's just notify that the insertion didn't
                // work, but let' s continue on ...
                errorLabel.Text = "There was a problem saving your quiz results into our database.  Therefore, the results from this quiz will not be displayed on the list on the main menu.";
            }
        }

    }

    protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
        bShowDetailsView = true;
        answerDetails.Visible = bShowDetailsView;
    }

}

如何在没有选择按钮的情况下选择和取消选择GridView中的行

Ali,这是一件很常见的事情,一个简单的谷歌搜索就会返回几个关于如何实现它的优秀教程。

基本上,您可以将以下代码添加到RowDataBound事件中:

    if (e.row.RowType == DataControlRowType.DataRow) { 
        e.row.Attributes["onmouseover"] =  
           "this.style.cursor='hand';"; 
        e.row.Attributes["onmouseout"] =  
           "this.style.textDecoration='none';"; 
        // Set the last parameter to True 
        // to register for event validation. 
        e.row.Attributes["onclick"] =  
         ClientScript.GetPostBackClientHyperlink(CustomerGridView,  
            "Select$" + row.RowIndex, true); 
    } 

此处填写完整指南:http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx