将值设置为gridview的Item模板中的TextBox

时间:2014-05-09 11:07:37

标签: c# asp.net gridview

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

我的问题是我不知道如何为textbox中存在的gridview分配值。

如何使用dataReader或DataSet在我的textbox templates fields内的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();
        }

    }

4 个答案:

答案 0 :(得分:3)

您应该将DataTable列直接绑定到TextBox内的TemplateField,例如......

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

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

答案 1 :(得分: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();
                }

答案 2 :(得分:0)

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();
        }

    }

答案 3 :(得分:0)

使用简单选择和内部选择的一种简短方法,用于没有代码后置的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>