0

list<type>すべての行が編集可能 (Excel のように) であり、データソースとしてグリッドビューの実際の例を誰かが投稿できますか?

ユーザーがポストバック間でグリッドに入力したデータ (送信コントロールだけでなく、任意のコントロールによって生成される可能性があります) を管理し、リストまたはグリッドビューからデータを取得して、それを使用して別のテーブルを挿入または更新します (これは 1:1 リレーション / grid:sqltable の典型的なケースではありません)

また、グリッドビューで使用される行数を制御する 1 から 9 までの数字のドロップダウンリスト (自動ポストバック付き) があるため、ユーザーが 1 行から 5 行に変更された場合、4 つの新しい行がグリッドビューに追加され、ユーザーが 8 から変更された場合5行まで、最後の3行が削除されます...(ユーザーが現在入力しているデータを維持します)

事前に感謝

4

1 に答える 1

0

次のように解決しました: ASPX:

 <asp:DropDownList ID="ddlNItems" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlNItems_SelectedIndexChanged">
     <asp:ListItem Selected="True">1</asp:ListItem>
     <asp:ListItem>2</asp:ListItem>
     <asp:ListItem>3</asp:ListItem>
     <asp:ListItem>4</asp:ListItem>
     <asp:ListItem>5</asp:ListItem>
     <asp:ListItem>6</asp:ListItem>
     <asp:ListItem>7</asp:ListItem>
     <asp:ListItem>8</asp:ListItem>
     <asp:ListItem>9</asp:ListItem>
 </asp:DropDownList>

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True">
     <Columns>
         <asp:TemplateField HeaderText="Código">
             <ItemTemplate>
                 <asp:TextBox ID="txtCodMuestra" runat="server" Text='<%# Eval("cod") %>' /></ItemTemplate>
             </asp:TemplateField>
          <asp:TemplateField HeaderText="Tipo">
              <ItemTemplate>
                  <asp:DropDownList runat="server" ID="ddlTipo" SelectedValue='<%# Eval("Tipo") %>'>
                      <asp:ListItem Value="a">A</asp:ListItem>
                      <asp:ListItem Value="b">B</asp:ListItem>
                      <asp:ListItem Value="c">C</asp:ListItem>
                  </asp:DropDownList>
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
  </asp:GridView>

CS:

public class Itemx
{
    public string cod { get; set; }
    public string Tipo { get; set; }
}
public static List<Itemx> LItems
{
    get
    {
        return (List<Itemx>)Session["LItems"];
    }
    set
    {
        Session["LItems"]= value;
    }
}
void InitializeItems()
{
    LItems = new List<Itemx>();
    ConfigItems();
}
void ConfigItems()
{
    int numitems = Convert.ToInt32(ddlNItems.SelectedValue);


    if (LItems.Count > numitems )
    {
        for (int i = GridView1.Rows.Count; i > numitems; i--)
        {
            LItems.RemoveAt(i - 1);
        }
    }
    else if (LItems.Count < numitems )
    {
        for (int i = GridView1.Rows.Count; i < numitems; i++)
        {
            LItems.Add(new Itemx { Tipo = "a" });
        }
    }
    transferGridtoList();

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

    GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
}
private void transferGridtoList()
{
    for (int i = 0; i < LItems.Count && i < GridView1.Rows.Count; i++)
    {
        LItems[i].cod = ((TextBox)GridView1.Rows[i].Cells[0].Controls[1]).Text;
        LItems[i].Tipo = ((DropDownList)GridView1.Rows[i].Cells[1].Controls[1]).SelectedValue;
    }
}

protected void ddlNItems_SelectedIndexChanged(object sender, EventArgs e)
{
    ConfigItems();
}
于 2012-12-05T08:45:24.417 に答える