0

管理者による更新用にこれらの GridView を設定する必要があります。したがって、私は多くの従業員と各 GridView に多くのコースを持っているので、GridView を更新する最良の方法は、空白のフィールドをチェックボックスとして表示し、管理者が従業員の 1 人のレコードを更新したい場合、すべて彼がする必要があるのは、チェックボックスをオンにして更新ボタンをクリックすることだけです。空のフィールドをチェックボックスとして表示することはできましたが、チェックボックスをオンにした値をデータベースに投稿する方法がわかりません。

私が探しているシナリオは、システムに各 GridView のすべてのチェックボックスをチェックさせることです。以前にチェックされている場合は、そのままにしておきます。空で、今チェックしたばかりの場合は、従業員のレコードでデータベースに追加する必要があります。したがって、保存ボタンをクリックすると GridView が更新されます。私の元のコードは次のとおりです。

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>

                <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />

                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                                    SelectCommandType="StoredProcedure" SelectCommand="kbiReport">
                                    <%--FilterExpression="[Division] like '{0}%' and [Organization] like '{1}%'">--%>

                    <%--<FilterParameters>
                        <asp:ControlParameter ControlID="ddlDivision" Name="Division" 
                                                 PropertyName="SelectedValue" Type="String" />
                        <asp:ControlParameter ControlID="ddlOrganization" Name="Organization" 
                                                PropertyName="SelectedValue" Type="String" />
                    </FilterParameters>--%>

                    <SelectParameters>
                        <%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values 
                            of GroupID--%>
                        <asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
                    </SelectParameters>
                </asp:SqlDataSource>

                <asp:GridView ID="GridView1" runat="server" 
                                AllowSorting="True" 
                                CellPadding="3" 
                                DataSourceID="SqlDataSource1" 
                                CssClass="mGrid"
                                AlternatingRowStyle-CssClass="alt" 
                                RowStyle-HorizontalAlign="Center" 
                                OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <HeaderStyle Font-Bold = "true" ForeColor="Black"/> 
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />

                        <%--<asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/>
                        </ItemTemplate>
                        </asp:TemplateField>--%>
                    </Columns>
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

            </ItemTemplate>
        </asp:Repeater>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                           ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                           SelectCommand="SELECT DISTINCT GroupID FROM courses">
        </asp:SqlDataSource>

        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
        <br /><br />

コードビハインド:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell c in e.Row.Cells)
            {
                // Check if the cell vlaue = Yes
                // if it is Yes, the cell will be colored with Light Green 
                if (c.Text.Equals("Yes"))
                {
                    c.BackColor = System.Drawing.Color.LightGreen;
                }
            }    
        }

        // The following is for changing the color of headers in each GridView based on the value of the HiddenFild 
        // BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database 
        else if(e.Row.RowType == DataControlRowType.Header){
            switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
            {
                case "1":
                    for (int i = 5; i &lt; e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
                    break;

                case "2":
                    for (int i = 5; i &lt; e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
                    break;

                case "3":
                    for (int i = 5; i &lt; e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
                    break;
            }
        }}


        //For inserting checkboxes inside all courses buttons
        protected void GridView1_DataBound(object sender, EventArgs e){
            GridView GridView1 = (GridView)sender;
            foreach (GridViewRow objRow in GridView1.Rows)
            {
                for (int i = 5; i &lt; objRow.Cells.Count; i++){
                    CheckBox chkCheckBox = new CheckBox();
                    objRow.Cells[i].Controls.Add(chkCheckBox);
                    if (objRow.Cells[i].BackColor == System.Drawing.Color.LightGreen)
                        chkCheckBox.Checked = true;
                }

            }
        }


        //For updating the GridView (Save Button)
        protected void btnSave_Click(object sender, EventArgs e)
        {

        }

更新: btnSave_Click ボタンを次のように変更しました。

//For updating the GridView (Save Button)
        protected void btnSave_Click(object sender, EventArgs e)
        {
            GridView GridView1 = (GridView)sender;
            // Are there checked boxes?
            List<int> CheckBoxList = new List<int>();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                int courseid = (int)GridView1.DataKeys[i][0];
                CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
                if (checkBox.Checked)
                {
                    CheckBoxList.Add(courseid);
                }
            }
        }

しかし、次のエラーが発生しました: Sys.WebForms.PageRequestManagerServerErrorException: タイプ 'System.Web.UI.WebControls.Button' のオブジェクトをタイプ 'System.Web.UI.WebControls.GridView' にキャストできません

なぜこのエラーが発生したのかわかりません。誰でもこれで私を助けることができますか?

4

3 に答える 3

0
         protected void btnSaveRankChanges_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow grv in GridViewRankChanges.Rows)
                {
                    CheckBox changeRankCheck = (CheckBox)grv.Cells[0].FindControl("CheckBoxRankChange");
    if (changeRankCheck != null && changeRankCheck.Checked == true)
         {
              //Do your work for selected checkbox
        }
    }
}

それが役に立てば幸い。

于 2011-12-10T11:15:12.610 に答える
0

Visual Basic .net では、グリッド ビューをループすることがよくありました。例えば

Privtae Sub Button1_Click(sender As Object, e As System.EventArgs)
   Dim rows as gridviewrows

   For Each rows in gridview1.rows
     dim chkbox as new checkbox

      'since we find specific control with unique id so we use to find that control by        
      'using the method FindControl

      chkbox = rows.findcontrol("chkInGridview")

      if chkbox.checked then
         'your code goes here
      else
         'your code goes here
      end if

   Next
End Sub

追伸:

これをさまざまな方法で解決します。ネストされたグリッドビューを使用してみてください。

于 2011-12-10T11:23:38.223 に答える
0

のキャストが無効btnSave_Clickです。sender変数には Button オブジェクトの参照があります。Repeater.Itemsオブジェクトにアクセスするにはコレクションを使用する必要がありGridViewます。

コードは次のようになります。

protected void btnSave_Click(object sender, EventArgs e)
 {
   for(int i=0;i<Repeater1.Items.Count;i++)
    {            
        GridView GridView1 = (GridView)Repeater1.Items[i].FindControl("GridView1");
        List<int> CheckBoxList = new List<int>();
        for (int i = 0; i < GridView1.Rows.Count; i++)
         {
             int courseid = (int)GridView1.DataKeys[i][0];
             CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
             if (checkBox.Checked)
              {
               CheckBoxList.Add(courseid);
              }
           }
        }
   }
于 2011-12-10T11:05:56.153 に答える