1

3つのテキストボックスにデータを入力するためにデータを抽出しようとしているグリッドビューがあります...さまざまな方法を試しましたが、これを行う正しい方法に到達できません... gridviewは手動であるため、「editTemplate」はありません...誰かがこれを行う正しい方法で私に指示してもらえますか?これは私のグリッドビューがどのように見えるかです:

    <asp:GridView ID="GridView1" runat="server"  AllowPaging="True" 
                                EmptyDataText="No parts has been added to the model, please add a part."
                                BorderColor="Black" BorderStyle="Solid"  BorderWidth="2px" 
                                CellPadding="3" onrowcommand="GridView1_RowCommand">
                                <Columns>
                                <asp:TemplateField HeaderText="">
                                 <ItemTemplate>
                                   <asp:LinkButton ID="EditButton" runat="server" CssClass="EditButton" CommandName="Edit" Text="Edit" />
                                 </ItemTemplate>
                                 </asp:TemplateField>
                                 <asp:TemplateField HeaderText="">
                                 <ItemTemplate>
                                   <asp:LinkButton ID="DeleteButton" runat="server" CssClass="DeleteButton" CommandName="Delete" Text="Delete" />
                                 </ItemTemplate>
                                 </asp:TemplateField>
                                </Columns>
                            <HeaderStyle BackColor="#FFFFCC"  BorderColor="Black" BorderStyle="Solid" 
                                BorderWidth="2px" />
                        </asp:GridView>

次に、このグリッドビューに必要な列を追加した方法は次のとおりです。

     protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dt = new DataTable();
        MakeDataTable();
        EmptyDataString();
    }
    else
    {
        dt = (DataTable)ViewState["DataTable"];
    }
    ViewState["DataTable"] = dt; 

}

#region GridView

private void EmptyDataString()
{
    TemplateBuilder tmpEmptyDataTemplate = new TemplateBuilder();
    tmpEmptyDataTemplate.AppendLiteralString("No parts has been added to the model, please add a part.");
    GridView1.EmptyDataTemplate = tmpEmptyDataTemplate;
    GridView1.DataBind();
}

private void MakeDataTable()
{
    dt.Columns.Add("Item");
    dt.Columns.Add("Quantity");
    dt.Columns.Add("Price P/Quantity");
}

private void AddToDataTable()
{
    DataRow dr = dt.NewRow();
    dr["Item"] = txtPart.Text;
    dr["Quantity"] = numQuantity.Text;
    dr["Price P/Quantity"] = txtPricePQ.Text;
    dt.Rows.Add(dr);
}

private void BindGrid()
{
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void btnAddPart_Click(object sender, EventArgs e)
{
    AddToDataTable();
    BindGrid();
    ClearNewParts();
}
#endregion

そして、これらは私がこれをしようとしたいくつかの方法です:(私がこれを行うための次の方法に移ったときにすべてがコメントアウトされています.....

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  //  int index = Convert.ToInt32(e.CommandArgument);
    if (e.CommandName == "Edit")
    {
        if (GridView1.SelectedRow != null)
        {
            //dataTable.Rows[GridView1.SelectedRow].BeginEdit();
            //dataTable.Rows[GridView1.SelectedRow]["yourFieldName"] = newValue;
            //dataTable.Rows[GridView1.SelectedRow].EndEdit();
            //gridView.DataSource = dataTable;
        }
      //  GridViewRow row = GridView1.Rows[e.RowIndex];

       // Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;
      //  txtPart.Text = GridView1.SelectedRow.Cells[1].Text;
       // txtPart.Text = GridView1.Rows[index].Cells[2].Text;
       // Response.Write(gv.Rows[index].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
    }

グリッドビューからデータを抽出してテキストボックスにデータを入力する方法に問題があることはわかっていますが、方法がわかりません。...(私はまだジュニアプログラミングであり、基本的に初めての作業です。ここで達成しようとしています)...

4

1 に答える 1

1

次のコードを使用してください>>

Windowsアプリの場合>>

for(int i=0;i<gv.Rows.Count;i++)
{
       id=gv.selectedRows[i].cells[0][0].value.toString();
       name=gv.selectedRows[i].cells[0][0].value.toString();
}

さもないと

これにより、グリッドビューから選択した行を取得できます。

選択せずに必要な場合は、

for(int i=0;i<gv.Rows.Count;i++)
    {
           id=gv.Rows[i].cells[0][0].value.toString();
           name=gv.Rows[i].cells[0][0].value.toString();
    }

3番目の選択肢>>

     if (e.CommandName == "Edit")
        {
            if (GridView1.SelectedRow != null)
            {
                //dataTable.Rows[GridView1.SelectedRow].BeginEdit();
                //dataTable.Rows[GridView1.SelectedRow]["yourFieldName"] = newValue;
                //dataTable.Rows[GridView1.SelectedRow].EndEdit();
                //gridView.DataSource = dataTable;
            }
          //  GridViewRow row = GridView1.Rows[e.RowIndex];
    for(int i=0;i<GridView1.Rows.Count;i++)
{
            Department = ((GridViewRow(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text;
            txtPart.Text = GridView1.SelectedRow[i][0].Cells[1].Text;
           txtPart.Text = GridView1.Rows[i][0].Cells[2].Text;
           Response.Write(gv.Rows[i][0].Cells[0].Text); //consider you use bound field and the column you want to show its value is the first column.
        }
}

さらに、これらの変数をテキストボックスに設定できます。

または直接設定することもできます。

試してみる。

于 2013-03-11T07:23:38.587 に答える