0

textboexに値を入力して追加ボタンを押すと、2つのテキストボックス、3つのボタン、2つのグリッドビューがあり、データテーブルに値が追加され、グリッドビューが表示され、送信ボタンをクリックすると、次のgirdviewでその詳細を表示できます。最初のグリッド ビューから任意の行を選択し、削除ボタンをクリックして、最初のグリッド ビューからその行を削除します。それまでは、すべてのコードが正常に機能します。最初のグリッド ビューから行を削除して送信ボタンを押すと、そのデータのみを表示できます。 1 番目のグリッドビューに存在する 2 番目のグリッドビュー

ここに私のコードがあります:

private void BindGrid(int rowcount)
{
     DataTable dt = new DataTable();
     DataRow dr;

     dt.Columns.Add("First Name", typeof(String));
     dt.Columns.Add("Last Name", typeof(String));

     if (ViewState["CurrentData"] != null)
     {
        for (int i = 0; i < rowcount + 1; i++)
        {
            dt = (DataTable)ViewState["CurrentData"];
            if (dt.Rows.Count > 0)
            {
                dr = dt.NewRow();
                dr[0] = dt.Rows[0][0].ToString();

            }
        }
        dr = dt.NewRow();
        dr[0] = TextBox1.Text;
        dr[1] = TextBox2.Text;
        dt.Rows.Add(dr);
    }
    else
    {
        dr = dt.NewRow();
        dr[0] = TextBox1.Text;
        dr[1] = TextBox2.Text;


        dt.Rows.Add(dr);

    }

     // If ViewState has a data then use the value as the DataSource
     if (ViewState["CurrentData"] != null)
     {
         GridView1.DataSource = (DataTable)ViewState["CurrentData"];
         GridView1.DataBind();

    }
     else
    {
         // Bind GridView with the initial data assocaited in the DataTable
         GridView1.DataSource = dt;
         GridView1.DataBind();
    }
     // Store the DataTable in ViewState to retain the values
      ViewState["CurrentData"] = dt;

 }

追加ボタンのクリックイベント:

protected void Button1_Click(object sender, EventArgs e)
     {
         // Check if the ViewState has a data assoiciated within it. If
         if (ViewState["CurrentData"] != null)
        {
             DataTable dt = (DataTable)ViewState["CurrentData"];
             int count = dt.Rows.Count;
             BindGrid(count);
         }
         else
         {
            BindGrid(1);
         }
          TextBox1.Text = string.Empty;
          TextBox2.Text = string.Empty;
          TextBox1.Focus();
      }
}

送信ボタン イベント:

if (ViewState["CurrentData"] != null)
        {
            GridView2.DataSource = (DataTable)ViewState["CurrentData"];
            GridView2.DataBind();
        }

ボタンイベントの削除:

protected void DeleteButton_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
       {
           CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
           if (cb != null && cb.Checked)
           {
               row.Visible = false;
           }
           else
           {
                Response.Write("Select check box to Delete");
           }
       }
   }

これは私のaspxページコードです:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
  <title></title>
  </head>
  <body>
     <form id="form1" runat="server">
     <div>
     <asp:TextBox ID="TextBox1" runat="server"/>&nbsp;&nbsp;&nbsp;&nbsp;
  <asp:TextBox ID="TextBox2" runat="server"/>&nbsp;&nbsp;&nbsp;<asp:DropDownList 
              ID="DropDownList1" runat="server">
              <asp:ListItem>ADT</asp:ListItem>
              <asp:ListItem>INF</asp:ListItem>
              <asp:ListItem>SC</asp:ListItem>
           </asp:DropDownList>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
  &nbsp;&nbsp;&nbsp;&nbsp;<br />
          &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
          <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
             GridLines="None">
             <AlternatingRowStyle BackColor="White" />
             <EditRowStyle BackColor="#2461BF" />
             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
              <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
             <RowStyle BackColor="#EFF3FB" />
             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
             <SortedAscendingCellStyle BackColor="#F5F7FB" />
             <SortedAscendingHeaderStyle BackColor="#6D95E1" />
             <SortedDescendingCellStyle BackColor="#E9EBEF" />
             <SortedDescendingHeaderStyle BackColor="#4870BE" />
             <Columns>
            <asp:TemplateField>
             <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server"  />
             </ItemTemplate>
        </asp:TemplateField>
     </Columns>

         </asp:GridView>

         <br />
         <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
             Text="Submit" />
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <asp:Button ID="DeleteButton" runat="server" Text="Delete" 
              onclick="DeleteButton_Click" />
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

          <br />
      <br />
         <asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" 
             GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
         </asp:GridView>
        </div>
     </form>
 </body>
</html>

前もって感謝します...

4

1 に答える 1

1

まず、データを削除するのではなくDataTable、DataGridView1 の行を非表示にするだけです。

削除する場合は、削除イベントで次のようにします。

var dt = (DataTable)ViewState["CurrentData"];
if (dt != null)
{
     var firstname = (row.FindControl("labelFirstName") as Label).Text; //my guess that you are binding DataRows to labels in grid view? if not this has to be changed
     var lastname = (row.FindControl("labelLastName") as Label).Text; //same as above

     var row = dt.AsEnumerable().FirstOrDefault(x => x.Field<string>("First Name") == firstname && x.Field<string>("Last Name") == lastname);

     dt.Rows.Remove(row);     

     GridView1.DataSource = dt;
     GridView1.DataBind();

     GridView2.DataSource = dt;
     GridView2.DataBind();

     ViewState["CurrentData"] = dt;
}

これは、DataTable から行を削除する方法です。

ただし、それらのみを非表示にしたい場合は、DataGridView2 で同じデータを持つ行を見つけて、それも非表示にする必要があります。

foreach (GridViewRow row in GridView1.Rows)
{
    CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
    if (cb != null && cb.Checked)
    {
        row.Visible = false;
        var firstname = (row.FindControl("labelFirstName") as Label).Text; //my guess that you are binding DataRows to labels in grid view? if not this has to be changed
        var lastname = (row.FindControl("labelLastName") as Label).Text; //same as above

        foreach (GridViewRow row in GridView2.Rows)
        {
             var found = false;
             // logic to search for row
             if (found)
             {
                  row.Visible = false;
             }
        }
    }
    else
    {
        Response.Write("Select check box to Delete");
    }
}

私はあなたの質問を正しく理解したことを願っています。それがあなたが望んでいたことです:)。そうでない場合は、コメントを書いてください。さらにお手伝いします:)


これはうまくいくはずです。

protected void DeleteButton_Click(object sender, EventArgs e)
{
    var dt = (DataTable)ViewState["CurrentData"];

    if (dt == null)
    {
        return;
    }

    List<DataRow> rowsToDelete = new List<DataRow>();
    foreach (GridViewRow row in GridView1.Rows)
    {        
        CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
        if (cb != null && cb.Checked)
        {
            row.Visible = false;

            //remove row by its index as it should GridViewRow index == DataRow index
            //it is not the best way but from your code I dont have information how your GridView looks
            rowsToDelete.Add(dt.Rows[row.RowIndex]);
        }
        else
        {
            Response.Write("Select check box to Delete");
        }
    }

    for (int i = 0; rowsToDelete.Count; i++)
    {
        dt.Rows.Remove(rowsToDelete[i]);
    }
}

この編集は正しいです。最後の編集はそれほど多くありませんが、削除する行がさらにある可能性があることを見落としていました:)。

DataBound のデータを変更するには、それ自体GridViewを変更する必要がDataSourceあります。GridViewそのため、アイテムを削除DataTableし、両方のグリッドで行われた変更に DataSource を再バインドする必要があります:)

于 2013-02-08T08:42:27.347 に答える