In this article I will
explain how to fetch data from a GridView and how to store it into a DataTable in ASP.NET
Fallow the below steps:
First, we have to
create GridView and set its AutoGenerateColumns property to False.
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 CreateDynamicTable()
{
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 = "BalQty";
dc.DataType
= Type.GetType("System.Decimal");
dtDynamic.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "QuantityIssued";
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 lbMaterialDesc_OnSelectedIndexChanged(object
sender, EventArgs e)
{
CreateDynamicTable();
DataRow dr = dtDynamic.NewRow();
dr["MatTransSlNo"] = 0;
dr["MaterialId"] = Convert.ToInt32(lbMaterialDesc.SelectedValue);
dr["MaterialDesc"] = MaterialDescName;
dr["UOMShortDesc"] = UOM;
dr["BalQty"] = Convert.ToInt32(dsBalanceQty.Tables[0].Rows[0]["BalQty"]);
dr["QuantityIssued"] = 0;
dr["DeleteFlag"] = 0;
dtDynamic.Rows.Add(dr);
GridView1.DataSource = dtDynamic;
GridView1.DataBind();
}
No comments:
Post a Comment