Question

I'm working with Outward Challan Detail, in which I need to show the results on a form by a gridview.

My problem is that I don't know how to assign values to textbox existing in the gridview.

How could I assign values in the textbox inside my templates fields that are in my gridview by using dataReader or DataSet?


Here is my 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>

Here is my C# code

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

    }
Was it helpful?

Solution

You should directly bind the DataTable Column to TextBox inside the TemplateField like...

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

This directly binds the values to TextBoxes. You can do this for all other TextBoxes.

OTHER TIPS

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

    }

A short way with Simple and Inner Select, For use in ASPX code with no Code Behind:

....

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top