Friday, 17 April 2015

How to get / fetch data from GridView and store in DataTable Dynamically in ASP.NET using C#.NET?

How to get / fetch data from GridView and store in DataTable Dynamically in ASP.NET using C#.NET?

In this article I will explain how to fetch / get data from a GridView and how to store it into a DataTable dynamically using C#.NET in ASP.NET

Fallow the below steps:

First, we have to create GridView with required Columns and set its AutoGenerateColumns property to False. If you required footer then show footer is true otherwise false and same as paging also.

Design the GridViewas per your reqirements. Use Templates like Item Template, Edit Item Template, and Footer Templates to Create GridView.


Design:

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="true"
                                                Width="100%" DataKeyNames="MatTransSlNo">
                                                <Columns>
                                                    <asp:TemplateField HeaderText="Material" SortExpression="StateID" ItemStyle-Width="35%" ItemStyle-HorizontalAlign="Left">
                                                        <ItemTemplate>
                                                            <asp:Label ID="lblMatTransSlNo" runat="server" Text='<%#Eval("MatTransSlNo") %>' Visible="false"></asp:Label>
                                                            <asp:Label ID="lblMaterialId" runat="server" Text='<%#Eval("MaterialId") %>' Visible="false"></asp:Label>
                                                            <asp:Label ID="lblMaterialIT" runat="server" Text='<%#Eval("MaterialDesc") %>' Width="90%"></asp:Label>
                                                        </ItemTemplate>
                                                        <FooterTemplate>
                                                            <asp:TextBox ID="txtMaterialFT" runat="server" AutoPostBack="true" OnTextChanged="txtMaterial_OnTextChanged"
                                                                Width="90%"></asp:TextBox>
                                                        </FooterTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="UOM" SortExpression="StateName" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Left">
                                                        <ItemTemplate>
                                                            <asp:Label ID="txtUOMIT" runat="server" Text='<%#Bind("UOMShortDesc") %>'></asp:Label>
                                                        </ItemTemplate>
                                                        <FooterTemplate>
                                                            <asp:Label ID="txtUOMFT" runat="server" Width="90%"></asp:Label>
                                                        </FooterTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Balance Quantity" SortExpression="Quantity" ItemStyle-Width="10%">
                                                        <ItemTemplate>
                                                            <asp:TextBox ID="lblQuantityIT" runat="server" Text='<%#Eval("BalQty") %>' Width="90%" MaxLength="9" Enabled="false"></asp:TextBox>
                                                            <asp:RequiredFieldValidator runat="server" ID="rfvQuantityIT" ControlToValidate="lblQuantityIT"
                                                                Display="Dynamic" ForeColor="Red" ErrorMessage="Quantity is required" ValidationGroup="CheckSave"></asp:RequiredFieldValidator>
                                                            <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtenderQuantity" runat="server" TargetControlID="lblQuantityIT"
                                                                FilterType="Custom, Numbers" ValidChars="." Enabled="True" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Quantity Issued" SortExpression="Rate" ItemStyle-Width="10%">
                                                        <ItemTemplate>
                                                            <asp:TextBox ID="lblQuantityIssuedIT" runat="server" Text='<%#Eval("QuantityIssued") %>' Width="90%" onblur='javascript:return ValidateGrid(this);' MaxLength="9"></asp:TextBox>
                                                            <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtenderRateIT" runat="server" TargetControlID="lblQuantityIssuedIT"
                                                                FilterType="Custom, Numbers" ValidChars="." Enabled="True" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Delete" ItemStyle-Width="10%">
                                                        <ItemTemplate>
                                                            <asp:CheckBox runat="server" ID="cbEdit" Checked='<%# Convert.ToBoolean(Eval("DeleteFlag")) %>' />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
</asp:GridView>

Code Behind:

Declare a DataTable in page load

DataTable dtDynamic = new DataTable();

Declare a DataTable with DataTypes

void CreateSaveDynamicTable()
    {

        DataColumn dc = new DataColumn();

        dc = new DataColumn();
        dc.ColumnName = "MatTransSlNo";
        dc.DataType = Type.GetType("System.String");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "MaterialId";
        dc.DataType = Type.GetType("System.String");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "MaterialDesc";
        dc.DataType = Type.GetType("System.String");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "UOMShortDesc";
        dc.DataType = Type.GetType("System.String");
        dtDynamic.Columns.Add(dc);


        dc = new DataColumn();
        dc.ColumnName = "TransQty";
        dc.DataType = Type.GetType("System.Decimal");
        dtDynamic.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "DeleteFlag";
        dc.DataType = Type.GetType("System.Int32");
        dtDynamic.Columns.Add(dc);

    }

protected void btnSave_OnClick(object sender, EventArgs e)
    {
        try
        {
            CreateSaveDynamicTable();
            int i = 1;
            foreach (GridViewRow r in GridView1.Rows)
            {
                Label MatTransSlNo = (Label)r.FindControl("lblMatTransSlNo");
                Label MaterialId = (Label)r.FindControl("lblMaterialId");
                Label lblMaterialIT = (Label)r.FindControl("lblMaterialIT");
                Label txtUOMIT = (Label)r.FindControl("txtUOMIT");

                TextBox lblQuantityIT = (TextBox)r.FindControl("lblQuantityIT");
                TextBox lblQuantityIssuedIT = (TextBox)r.FindControl("lblQuantityIssuedIT");


                CheckBox cbDelete = (CheckBox)r.FindControl("cbEdit");

                DataRow dr = dtDynamic.NewRow();
                dr["MatTransSlNo"] = i;
                dr["MaterialId"] = Convert.ToInt32(MaterialId.Text);
                dr["MaterialDesc"] = lblMaterialIT.Text;
                dr["UOMShortDesc"] = txtUOMIT.Text;

                if (lblQuantityIssuedIT.Text != string.Empty)
                {
                    dr["TransQty"] = Convert.ToDecimal(lblQuantityIssuedIT.Text);
                }
                else
                {
                    dr["TransQty"] = 0;
                }

                if (cbDelete.Checked == true)
                {
                    dr["DeleteFlag"] = 1;
                }
                else
                {
                    dr["DeleteFlag"] = 0;
                }

                dtDynamic.Rows.Add(dr);
                i++;
            }

            ViewState["Dynamic"] = dtDynamic;

            DataSet dsSaveMaterial = new DataSet();
            dsSaveMaterial.Tables.Add(dtDynamic);

            string GridData = dsSaveMaterial.GetXml();
           
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }

    }

No comments:

Post a Comment