【问题标题】:set values to TextBox in Item template of gridview在gridview的Item模板中为TextBox设置值
【发布时间】:2014-05-09 11:07:36
【问题描述】:

我正在使用 Outward Challan Detail,其中我需要通过 gridview 在表单上显示结果。

我的问题是我不知道如何将值分配给存在于gridview 中的textbox

如何使用 dataReader 或 DataSet 在 templates fields 中的 textbox 中分配 gridview 中的值?


这是我的 aspx

<div id="OutDCItemDetails" runat="server" style="overflow:auto">
<asp:Panel ID="PanelOutDCItemDetails" runat="server">
    <asp:GridView ID="gvOutDCItemDetails" runat="server" AllowPaging="True" 
        PageSize="6" AutoGenerateColumns="False" 
        onrowdatabound="gvOutDCItemDetails_RowDataBound" 
        onrowcommand="gvOutDCItemDetails_RowCommand" 
        onselectedindexchanged="gvOutDCItemDetails_SelectedIndexChanged" 
        BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" 
        CellPadding="3" CellSpacing="1" GridLines="None" DataKeyNames="Item_Id" >
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField HeaderText="Item Id" DataField="Item_Id" />
            <asp:BoundField HeaderText="Item Name" DataField="IName" />
            <asp:BoundField HeaderText="Net Quantity" DataField="I_Quantity" />
            <asp:BoundField DataField="Remaining_Qty" HeaderText="Remaining Quantity" />
            <asp:TemplateField HeaderText="Process">
            <ItemTemplate>
            <asp:DropDownList ID="ddrProcess" runat="server" >
                </asp:DropDownList>
            </ItemTemplate>          

            </asp:TemplateField>
            <asp:TemplateField HeaderText="Dispatch Quantity">          
            <ItemTemplate>
                <asp:TextBox ID="txtDispatchQuantity" runat="server" AutoPostBack="true" OnTextChanged="TextChanged_txtDispatchQuantity"></asp:TextBox></ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Remaining Quantity">          
            <ItemTemplate>
                <asp:TextBox ID="txtRamainingQuantity" runat="server"></asp:TextBox></ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Rate">          
            <ItemTemplate>
                <asp:TextBox ID="txtRate" runat="server" AutoPostBack="true" OnTextChanged="txtRate_TextChanged"></asp:TextBox></ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Amount">          
            <ItemTemplate>
                <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox></ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="lblStatus" runat="server" Text="Status"></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#594B9C" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#33276A" />
    </asp:GridView>

这是我的C#代码

 protected void gvOutDC_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (gvOutDC.SelectedIndex >= 0)
        {
            btnsave.Enabled = false;
            btnInword.Visible = false;

            OutDC.Visible = true;
            OutDCItemDetails.Visible = true;
            View.Visible = false;
            InwordDetails.Visible = false;
            txtOutId.Visible = true;
            txtoutCode.Enabled = false;
            btn.Visible = true;
            txtcustcode.Enabled = false;
            btnsave.Enabled = true;


            txtOutId.Text = gvOutDC.SelectedDataKey[0].ToString();

            txtoutCode.Text = gvOutDC.SelectedRow.Cells[2].Text.ToString();
            txtDate.Text =gvOutDC.SelectedRow.Cells[8].Text.ToString();
            txtCustomerId.Text = gvOutDC.SelectedRow.Cells[5].Text.ToString();
            txtcustcode.Text = gvOutDC.SelectedRow.Cells[7].Text.ToString();
            txtCustomerName.Text = gvOutDC.SelectedRow.Cells[6].Text.ToString();
            int inworditem = Convert.ToInt16(gvOutDC.SelectedRow.Cells[3].Text.ToString());
            SqlCommand cmd = new SqlCommand("sp_getOutDCmaterialDetail",con1);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@outDCid", txtOutId.Text);
            cmd.Parameters.AddWithValue("@inwordItem", inworditem);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

           //.Text = ds.Tables[0].Rows[0][0].ToString();
            con1.Open();
            //SqlDataReader dr=cmd.ExecuteReader();
            //if (dr.HasRows)
            //{
            //    while (dr.Read())
            //    {

            //    }
            //}

            gvOutDCItemDetails.DataSource = ds;
            gvOutDCItemDetails.DataBind();

            OutDCItemDetails.Visible = true;


        }
    }

   protected void gvOutDCItemDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            //{
                DropDownList ddList = (DropDownList)e.Row.FindControl("ddrProcess");
                //bind dropdownlist

                SqlCommand cmd = new SqlCommand("sp_getProcess", con1);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];
                //DataTable dt = con1.GetData("Select category_name from category");
                ddList.DataSource = dt;
                ddList.DataTextField = "PName";
                ddList.DataValueField = "Process_Id";
                ddList.DataBind();
                ddList.Items.Insert(0,new ListItem("--SELECT--","0"));

                TextBox txtDispatchQuantity = (TextBox)e.Row.FindControl("txtDispatchQuantity");
                txtDispatchQuantity.Text = ds.Tables[0].Rows[0][3].ToString();

                TextBox txtRamainingQuantity = (TextBox)e.Row.FindControl("txtRamainingQuantity");
                txtRamainingQuantity.Text = ds.Tables[0].Rows[0][3].ToString();

                TextBox txtRate = (TextBox)e.Row.FindControl("txtRate");
                txtRate.Text = ds.Tables[0].Rows[0][3].ToString();


                TextBox txtAmount = (TextBox)e.Row.FindControl("txtAmount");
                txtAmount.Text = ds.Tables[0].Rows[0][3].ToString();


                }
        if (e.Row.RowType == DataControlRowType.Footer)
        {

          //  Label lblTotalPrice = (Label)e.Row.FindControl("Total_Amount");

            //lblTotalPrice.Text = total.ToString();
           // txttotalAmount.Text = Total.ToString();
        }

    }

【问题讨论】:

  • ds.Tables[0].Rows[0][3].ToString(); 空了?
  • 找不到第 3 列。
  • 我执行这个任务是为了更新记录

标签: c# asp.net gridview


【解决方案1】:

您应该直接将DataTable 列绑定到TextBox 内的TemplateField 之类...

<asp:TextBox ID="txtDispatchQuantity" runat="server" Text='<%# Eval("ColumnNameInDataSetTable") %>' />

这会将值直接绑定到TextBoxes。您可以对所有其他 TextBoxes 执行此操作。

【讨论】:

  • 他在使用RowDataBound 是否需要将其绑定到网格本身?
  • @MichelAyres 如果他直接绑定到 Markup,那么就不需要使用RowDataBound。他在那里写了很多代码,我猜是不需要的。
  • 你是对的。我看到lblTotalPrice 并认为他正在以某种方式操纵它。但他不是......绑定到网格是一个很好的方法。
  • 谢谢@MichelAyres。 :)
  • 不错,很聪明
【解决方案2】:
TextBox txtDispatchQuantity = (TextBox)e.Row.FindControl("txtDispatchQuantity");

                var dataRow = (DataRowView)e.Row.DataItem;
                var Dispatch_Qty = "Dispatch_Qty";
                var check = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(Dispatch_Qty, StringComparison.InvariantCultureIgnoreCase));
                if (check)
                {
                    // Property available
                    txtDispatchQuantity.Text =ds1.Tables[0].Rows[0][7].ToString();
                }

【讨论】:

    【解决方案3】:
    protected void gvOutDCItemDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                //{
                    DropDownList ddList = (DropDownList)e.Row.FindControl("ddrProcess");
                    //bind dropdownlist
    
                    SqlCommand cmd = new SqlCommand("sp_getProcess", con1);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataTable dt = ds.Tables[0];
                    //DataTable dt = con1.GetData("Select category_name from category");
                    ddList.DataSource = dt;
                    ddList.DataTextField = "PName";
                    ddList.DataValueField = "Process_Id";
                    ddList.DataBind();
                    ddList.Items.Insert(0,new ListItem("--SELECT--","0"));
    
                    TextBox txtDispatchQuantity = (TextBox)e.Row.FindControl("txtDispatchQuantity");
    
                    var dataRow = (DataRowView)e.Row.DataItem;
                    var Dispatch_Qty = "Dispatch_Qty";
                    var check = dataRow.Row.Table.Columns.Cast<DataColumn>().Any(x => x.ColumnName.Equals(Dispatch_Qty, StringComparison.InvariantCultureIgnoreCase));
                    if (check)
                    {
                        // Property available
                        txtDispatchQuantity.Text =ds1.Tables[0].Rows[0][7].ToString();
                    }
    
                    }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
    
              //  Label lblTotalPrice = (Label)e.Row.FindControl("Total_Amount");
    
                //lblTotalPrice.Text = total.ToString();
               // txttotalAmount.Text = Total.ToString();
            }
    
        }
    

    【讨论】:

      【解决方案4】:

      简单和内部选择的捷径,用于无代码隐藏的 ASPX 代码:

      ....
      
      <ItemTemplate>
          <tr>
                  <td>
                          <asp:Label ID="Label1" runat="server" Text='<%# Eval("COrder") %>' />
                      </td>
                      <td>
                          <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("CText") %>' />
                      </td>
                      <td>
                          <a href='<%# "HomeProducts.aspx?Delete=" & Eval("RId")%>' class="btn btn-danger btn-sm RedBtn btn-delete">Delete</a>
                      </td>
               </tr>
      </ItemTemplate>
      
      ....
      
      <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [RId],[CId],[COrder],[CText]=(SELECT [Title] from [Categories] where [ID]=[HomeProduct].[CId]) FROM [HomeProduct] ORDER BY [COrder] DESC"></asp:SqlDataSource>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2016-03-03
        • 2021-07-29
        • 1970-01-01
        • 2018-05-24
        • 1970-01-01
        相关资源
        最近更新 更多