2

asp.netでGridViewを使用しています。単一のデータ行を選択したい。プロパティ パネルで MultiSelect と SelectionMode を探しましたが、見つかりません。

では、GridView で行を選択できるようにするにはどうすればよいでしょうか。

ありがとう。

ここに画像の説明を入力 コードビハインド

public partial class SearchCourse : System.Web.UI.Page
{
Connection dbCon;
DataTable tbl;

protected void Page_Load(object sender, EventArgs e)
{
    dbCon = new Connection();


}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton1.Checked) {
        txtSubName.Enabled = true;
        comboSemester.Enabled = false;
        comboYear.Enabled = false;
        comboProgram.Enabled =false;
        txtSubName.Text = "";
    }
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton2.Checked) {

        comboProgram.Enabled = true;

        if (comboProgram.SelectedItem.ToString() == "Foundation Course")
        {
            comboSemester.Enabled = false;
            comboYear.Enabled = false;
        }
        else {
            comboSemester.Enabled = true;
            comboYear.Enabled = true;
        }
        txtSubName.Text = "";
        txtSubName.Enabled = false;
    }
}

protected void imgBtnSearch_Click(object sender, ImageClickEventArgs e)
{
    if (RadioButton1.Checked) {
        String name = txtSubName.Text;
        tbl = dbCon.getResultsBySubjectName(name);
        GridView1.DataSource = tbl;
        GridView1.DataBind();
    }
    else if (RadioButton2.Checked)
    {
        String program = comboProgram.SelectedItem.ToString();
        String year = comboYear.SelectedItem.ToString();
        String sem= comboSemester.SelectedItem.ToString();
        tbl = dbCon.getResultsByProgram(program,year,sem);
        GridView1.DataSource = tbl;
        GridView1.DataBind();
    }
    else if (RadioButton3.Checked)
        {
            String name = txtSubName.Text;
            tbl = dbCon.getResultsBySubjectNo(name);
            GridView1.DataSource = tbl;
            GridView1.DataBind();
        }

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    String program = comboProgram.SelectedItem.ToString();
    String year, sem;
    if (program == "Foundation Course")
    {
        comboYear.Enabled = false;
        comboSemester.Enabled = false;
        year = null;
        sem = null;
    }
    else {
        comboYear.Enabled = true;
        comboSemester.Enabled = true;
        year = comboYear.SelectedItem.ToString();
        sem = comboSemester.SelectedItem.ToString();
    }

    tbl = dbCon.getResultsByProgram(program, year, sem);
    GridView1.DataSource = tbl;
    GridView1.DataBind();
}

protected void comboYear_SelectedIndexChanged(object sender, EventArgs e)
{
    String program = comboProgram.SelectedItem.ToString();
    String year = comboYear.SelectedItem.ToString();
    String sem = comboSemester.SelectedItem.ToString();
    tbl = dbCon.getResultsByProgram(program, year, sem);
    GridView1.DataSource = tbl;
    GridView1.DataBind();
}
protected void comboSemester_SelectedIndexChanged(object sender, EventArgs e)
{
    String program = comboProgram.SelectedItem.ToString();
    String year = comboYear.SelectedItem.ToString();
    String sem = comboSemester.SelectedItem.ToString();
    tbl = dbCon.getResultsByProgram(program, year, sem);
    GridView1.DataSource = tbl;
    GridView1.DataBind();

}



protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
{
    if (RadioButton3.Checked)
    {
        txtSubName.Enabled = true;
        comboSemester.Enabled = false;
        comboYear.Enabled = false;
        comboProgram.Enabled = false;
        txtSubName.Text = "";
    }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

GridView コード

<asp:GridView ID="GridView1" CssClass="grid" runat="server" AllowPaging="True" 
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" 
GridLines="Horizontal"  EnableViewState="False" 
PageSize="5"   onselectedindexchanged="GridView1_SelectedIndexChanged" >

<RowStyle CssClass="gridRow" Width="800px" />

<SelectedRowStyle BackColor="#FF0066" ForeColor="White" />

</asp:GridView> 
4

2 に答える 2

0

MultiSelect および SelectionMode プロパティは、ASP.NET ではなく、VB.NET グリッドでのみ使用できると思います。ASP.NET のすべてのコントロールは偽装された HTML であるため、より制限される可能性があることに注意してください。複数選択テーブルを使用できない理由はありませんが、自分で配管を行う必要があります。したがって、RowDataBound イベントを次のように処理して、行の選択を有効にする必要があります。

http://forums.asp.net/t/992062.aspx?How+to+select+row+in+gridview+on+click

または、MS 提供のオプションを次のように使用します。

http://msdn.microsoft.com/en-us/library/wbk82279(v=vs.100).aspx

次に、SelectedIndexChanging イベントを処理し、ユーザーがクリックした行を特定し、行の色付けを自分で処理する必要があります。

于 2014-02-24T09:48:11.163 に答える
-1

gridview では、以下のようにイベントonselectedindexchangedを定義する必要があります。onrowdatabound

onselectedindexchanged="GridView1_SelectedIndexChanged"  onrowdatabound="GridView1_RowDataBound"  

選択した行を表示するには、グリッド ビューで次のスタイルを使用できます。

 <SelectedRowStyle BackColor="Red" />

コードビハインド:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Set the hand mouse cursor for the selected row.
        e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");

        // The seelctButton exists for ensuring the selection functionality
        // and bind it with the appropriate event hanlder.
        LinkButton selectButton = new LinkButton()
        {
            CommandName = "Select",
            Text = e.Row.Cells[0].Text
        };
        selectButton.Font.Underline = false;
        selectButton.ForeColor = Color.Black;

        e.Row.Cells[0].Controls.Add(selectButton);
        //e.Row.Attributes["OnClick"] =
        //     Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
        e.Row.Attributes["onclick"] =         ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
    }

注:イベント ウィンドウでイベントを見つけることができます。

于 2013-04-06T04:13:17.890 に答える