Filtering gridview with textbox in header - c#

Soo I'm trying to filter my gridview with textboxes in the header. I created a textchanged method for every textbox. Now I want to filter the gridview and bind the filtered data. For some reason the datasource is null once it hits the textchanged method. See my code below:
<asp:GridView ID="GridView_Imported" runat="server" CssClass="GridView-Upload" Width="100%" OnRowDataBound="GridView_Imported_RowDataBound" HorizontalAlign="Center" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" CssClass="checkAll" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkCtrl" runat="server" CssClass="chkCtrl" onclick="Check_Click(this)" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="filter">
<HeaderTemplate>
<asp:Label ID="lbArticleName" runat="server" Text="Article-Name:" CssClass="Article-Name-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterArticleName" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticleName_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="ArticleNameText" runat="server" Text='<%# Eval("Article-Name") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbArticle" runat="server" Text="Article:" CssClass="Article-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterArticle" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticle_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbArticleText" runat="server" Text='<%# Eval("Article") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbWarehouse" runat="server" Text="Warehouse:" CssClass="Warehouse-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterWarehouse" runat="server" placeholder="Filter..." OnTextChanged="tbFilterWarehouse_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbWarehouseText" runat="server" Text='<%# Eval("Warehouse") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbLocation" runat="server" Text="Location:" CssClass="Location-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterLocation" runat="server" placeholder="Filter..." OnTextChanged="tbFilterLocation_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbLocationText" runat="server" Text='<%# Eval("Location") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
protected void tbFilterArticleName_TextChanged(object sender, EventArgs e)
{
var tb = (TextBox) sender as TextBox;
GridView_Imported.DataSource = (Session["dataSource"] as DataTable).DefaultView.RowFilter = string.Format("Article-Name LIKE '%{0}%'", tb.Text.Trim());
GridView_Imported.DataBind();
}
What I'm missing?

There are two errors in your code.
First put brackets for column name Article-Name as [Article-Name].
Second the way you assigning datasource to gridview. See the correct way below.
var dt = (Session["dataSource"] as DataTable);
dt.DefaultView.RowFilter = string.Format("[Article-Name] LIKE '%{0}%'", tb.Text.Trim());
GridView_Imported.DataSource = dt;
GridView_Imported.DataBind();

Use http://www.datatables.net/
It provides data filtering, sorting, paging etc.
Just place a html table header above your gridview with some id
<HeaderTemplate>
<table id="tblData" border="0" cellpadding="5" cellspacing="1" width="100%">
<thead>
<th align="center" width="3%">S.No.
</th>
<th align="center" width="10%">RFQ ID
</th>
</thead>
</HeaderTemplate>
Add add jquery script
$(document).ready(function () {
$('#tblData').DataTable();
});

First, in RowDataBound event of your gridview(to find your textbox):
System.Web.UI.WebControls.TextBox tbFilterArticle;
protected void GridView_Imported_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (GridView_Imported.HeaderRow != null)
{
tbFilterArticle = (System.Web.UI.WebControls.TextBox)GridView_Imported.HeaderRow.FindControl("tbSearchUser");
}
}
After that, to use it's value(anywhere you want, i used it into a button click event):
string article = tbFilterArticle.Text;

Related

Get text box value from grid view Header

I have grid with text box for filter data on header.I have button outside the grid.I am using this gridview for filtering the grid by some columns. When I click on button I want to get value from text box and dropdownlist on codebehind.cs, below is my webform.aspx code of gridview,
<asp:Button ID="btngetLocationDate" runat="server" Text="Get Filtered Data" OnClick="getTextBoxValue"></asp:Button>
<asp:GridView ID="TrackerGrid" runat="server" BackColor="#CCCCCC" AllowPaging="True" AutoGenerateColumns="false">
<AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:TemplateField HeaderText="User ID" SortExpression="User ID">
<ItemTemplate>
<asp:Label ID="lbluserid" runat="server" Text='<%# Eval("User ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<HeaderTemplate>
Full Name:
<asp:DropDownList ID="ddlfullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="onselectDropdown">
<asp:ListItem Text="ALL" Value="ALL" Selected="True"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("Full Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<HeaderTemplate>
Mobile Number:
<asp:TextBox ID="txtMobilenumber" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%# Eval("Mobile Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address">
<HeaderTemplate>
Email Id:
<asp:TextBox ID="txtemail" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%# Eval("Email ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Longitude">
<HeaderTemplate>
<asp:Label ID="lbllng" runat="server" Text="Longitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllongi" runat="server" Text='<%# Eval("Longitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lattitude">
<HeaderTemplate>
<asp:Label ID="lbllat" runat="server" Text="Lattitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllatti" runat="server" Text='<%# Eval("Lattitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activated On">
<HeaderTemplate>
<asp:Label ID="lbldevices" runat="server" Text="Activated On"></asp:Label></br>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldevice" runat="server" Text='<%# Eval("Activated On") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location DateTime" SortExpression="Location DateTime">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Location DateTime"></asp:Label></br>
<asp:Label ID="lblStartDate" runat="server" AssociatedControlID="txtStartDate" Text="Start Date"></asp:Label>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarStartDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtStartDate" popupbuttonid="imgPopup1"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup1" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</br>
<asp:Label ID="lblEndDate" runat="server" Text="End Date" AssociatedControlID="txtEndDate"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarEndDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtEndDate" popupbuttonid="imgPopup2"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup2" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDatetime" runat="server" Text='<%# Eval("Location DateTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and I am getting texbox value in codebehind code.
protected void getTextBoxValue(object sender, EventArgs e)
{
DropDownList ddlfullname = (TrackerGrid.HeaderRow.FindControl("ddlfullName") as DropDownList);
string selectedValue = ddlfullname.SelectedItem.Text;
TextBox mobilenumber = (TrackerGrid.HeaderRow.FindControl("txtMobilenumber") as TextBox);
string mobile = mobilenumber.Text;
TextBox email = TrackerGrid.HeaderRow.FindControl("txtemail") as TextBox;
string emailid = email.Text;
TextBox staloc = TrackerGrid.HeaderRow.FindControl("txtStartDate") as TextBox;
string startlocatdate = staloc.Text;
TextBox endloc = TrackerGrid.HeaderRow.FindControl("txtEndDate") as TextBox;
string endlocadate = endloc.Text;
}
I am getting empty value when I debugging this code.I think my code is correct but how i am not getting value of textboxes, i don't understand. Please help me for solving my issue.
Thanks in advance.
You probably did not wrap the DataBind() of the Gridview inside an IspostBack check. If you do not then the values will be rested by the DataBind instead of being saved with ViewState.
protected void Page_Load(object sender, EventArgs e)
{
//not here
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
if (IsPostBack == false)
{
//but inside an ispostback check
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
}
}

How can use FindControl in the OnRowCommand of GridView?

I am trying to use findcontrol TextBox in RowCommand of the gridview.
But Error Object reference not set to an instance of an object.
Help me please.
Design
<asp:GridView ID="gvMaster" runat="server" AllowPaging="true" AutoGenerateColumns="False" OnRowCommand="gvMaster_RowCommand" Width="100%">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" runat="server" CommandName="edit" ImageUrl="~/images/edit.gif" ToolTip="Insert/Edit" />
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td nowrap="nowrap">
<asp:ImageButton ID="ibtnSave" runat="server" CommandName="update" ImageUrl="~/images/icon-floppy.gif" ToolTip="Save" />
</td>
<td nowrap="nowrap">
<asp:ImageButton ID="ibtnCancel" runat="server" CommandName="cancel" ImageUrl="~/images/icon-cancel.gif" ToolTip="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="30px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Effective Date">
<ItemTemplate>
<asp:Label ID="lblEffectiveDate" runat="server" Text='<%# String.Format("{0:dd/MM/yyyy}", Eval("eff_date")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td nowrap="nowrap">
<asp:TextBox ID="txtEffDate2" runat="server" MaxLength="10" Text='<%# String.Format("{0:dd/MM/yyyy}", Eval("eff_date")) %>'
Width="70px" Style="text-align: center"></asp:TextBox>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code BeHind:
protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToUpper().Equals("SELECT"))
{
}
else if (e.CommandName.ToUpper().Equals("EDIT"))
{
string cmdNmEdit = e.CommandName;
object cmdSrcEdit = e.CommandSource;
GridViewRow gvMaster = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
TextBox txtEffDate2 = gvMaster.FindControl("MyTextBoxId") as TextBox;
txtEffDate2.Text = DateTime.Now.ToString("DD/MM/yyyy"); //<------ Error This Line
}
}
How can use FindControl in the OnRowCommand of GridView?
Thanks in advance. ;)
The CommandArgument contains the row-index by default. That's why this works:
protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView) sender;
GridViewRow row = grid.Rows[rowIndex];
// now you can use row.FindControl
}

C# - Binding a GridView Eval data without a Data Source Control

I am looking to populate a (sorted) GridView (called SearchResultsGid) without the use of a data source control. I did some research and I am guessing I may need to use a data set and datable to successfully bind the the grid, but I am not entirely sure how to do it. I'm more used to using a data source control....
The grid view uses Eval statements, which I would like to keep if possible because it allows for the fields to be hyperlinks to other pages. Although, if there is another way, I would be willing to try it...
The point of the SearchResultsGrid is that, when the user types in something in a text field then hits the "search" button, a grid pops up with all relevant results from the database. There is a repeater which displays all database items that the user has added for viewing, but it is not relevant to me at the moment (it works just fine anyway).
Any help would be appreciated! :)
Here is the code:
<table cellpadding="4" cellspacing="0" class="inputForm" width="100%">
<tr><td width="100%"><p align="justify">Select the products that should have this option.</p></td></tr>
<tr>
<td valign="top">
<asp:Label ID="errorLbl" ForeColor="Red" Font-Bold="true" runat="server" Text=""></asp:Label>
<asp:UpdatePanel ID="MainContentAjax" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="section">
<div class="header"><h2><asp:Localize ID="FindProductsCaption" runat="server" Text="Find Products" /></h2></div>
<asp:Panel ID="SearchFormPanel" runat="server" CssClass="content" DefaultButton="SearchButton">
<table class="inputForm">
<tr>
<th class="rowHeader"><cb:ToolTipLabel ID="SearchNameLabel" runat="server" Text="Product Name:" ToolTip="Enter all or part of a product name. Wildcard characters * and ? are accepted." /></th>
<td><asp:TextBox ID="SearchName" runat="server" Text="" /></td>
<th class="rowHeader"><cb:ToolTipLabel ID="ShowImagesLabel" runat="server" Text="Show Thumbnails:" ToolTip="When checked, product images will be displayed in the search results." /></th>
<td><asp:CheckBox ID="ShowImages" runat="server" /></td>
<td><asp:LinkButton ID="SearchButton" runat="server" Text="Search" SkinID="Button" OnClick="SearchButton_Click" /></td>
</tr>
</table>
<cb:SortedGridView ID="SearchResultsGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductId" GridLines="None" SkinID="PagedList" Width="100%" AllowPaging="true" Visible="false" DefaultSortExpression="Name" EnableSortingAndPagingCallbacks="True" PageIndex="0" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Thumbnail">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:HyperLink ID="NodeImageLink" runat="server" NavigateUrl='<%# UrlGenerator.GetBrowseUrl((int)Eval("ProductId"), CatalogNodeType.Product, (string)Eval("Name")) %>'> <asp:Image ID="NodeImage" runat="server" ImageUrl='<%# Eval("ThumbnailUrl") %>' Visible='<%# !string.IsNullOrEmpty((string)Eval("ThumbnailUrl")) %>' AlternateText='<%# Eval("Name") %>' /> </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SKU" SortExpression="Sku">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:HyperLink ID="ProductSku" runat="server" Text='<%#Eval("Sku")%>' SkinID="FieldHeader" /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item" SortExpression="Name" >
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:HyperLink ID="ProductName" runat="server" Text='<%#Eval("Name")%>' SkinID="FieldHeader" /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned">
<ItemStyle Width="50px" HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="AttachButton" runat="server" CommandArgument='<%#Container.DataItemIndex%>' AlternateText="Add" ToolTip="Add" SkinID="AddIcon" OnClientClick="this.visible=false" OnClick="AttachButton_Click" Visible='<%#!IsProductLinked(((Product)Container.DataItem).ProductId)%>' />
<asp:ImageButton ID="RemoveButton" runat="server" CommandArgument='<%#Container.DataItemIndex%>' AlternateText="Remove" ToolTip="Remove" SkinID="DeleteIcon" OnClientClick="return confirm('Are you sure you want to remove this item from this option?')" OnClick="RemoveButton_Click" Visible='<%#IsProductLinked(((Product)Container.DataItem).ProductId)%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
There are no products that match the search text.
</EmptyDataTemplate>
</cb:SortedGridView>
</asp:Panel>
</div>
<div class="section">
<div class="header"><h2>Assigned Products</h2></div>
<div class="content">
<asp:Repeater ID="RelatedProductGrid" runat="server" SkinID="PagedList" OnItemCommand="RelatedProductGrid_RowDeleting">
<HeaderTemplate>
<table width="100%" class="pagedList" cellpadding="0" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr class="oddRow">
<td>
<asp:Label ID="ProdID" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
<asp:HyperLink ID="ProductName2" runat="server" Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
</td>
<td><asp:ImageButton ID="RemoveButton2" runat="server" SkinID="DeleteIcon" CommandName="Delete" CommandArgument='<%#Eval("ProductId") %>' AlternateText="Remove" ToolTip="Remove" OnClientClick="return confirm('Are you sure you want to remove this item from this option?')" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="evenRow">
<%-- <td>
<asp:Label ID="ProdID2" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
<asp:HyperLink ID="ProductSku" runat="server" Text='<%#Eval("Sku")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
</td>--%>
<td>
<asp:Label ID="ProdID" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
<asp:HyperLink ID="ProductName2" runat="server" Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
</td>
<td><asp:ImageButton ID="RemoveButton2" runat="server" SkinID="DeleteIcon" CommandName="Delete" CommandArgument='<%#Eval("ProductId") %>' AlternateText="Remove" ToolTip="Remove" /></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
<asp:Button ID="FinishButton" runat="server" Text="Finish" OnClick="FinishButton_Click" /><br />
<br />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
I started work on the function to populate the SearchResultsGrid, and the SQL command is how I want to be in terms of getting the correct information on the button click. I plan to call this function inside the button click.
protected void PopulateSearchGrid()
{
try
{
System.Data.SqlClient.SqlDataReader dr = null;
DataSet ds = new DataSet();
DataTable resultsTbl = ds.Tables.Add("resultsTbl");
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT OptionID, ac_Products.Name, Sku FROM ac_Options, ac_Products WHERE OptionID = #OptionID AND ac_Products.Name = #Name", cn))
{
///and this is where I'm getting confused on what to do :(
}
}
}
catch { }
}
You may use a DataTable object, populating it with a SQL query, and assign it to the datasource of the grid.
Use your DataAdapter to fill your DataTable with your SQL query (mind to match column names with those on your GridView)
DataTable dt = new DataTable();
//use your DataAdapter to fill the data table
sda.Fill(dt);
SearchResultsGrid.DataSource = dt;
SearchResultsGrid.DataBind();
Your query is using parameters, so you might want to use a SqlCommand instead of specifying the SQL directly on your SqlDataAdapter.
SqlCommand cmd = new SqlCommand("Your Query",cn);
cmd.Parameters.Add("Your first parameter", your param value);
cmd.Parameters.Add("Your second parameter", your param value);
... new SqlDataAdapter(cmd)...

Remove individual rows of a grid view only on the client side

I have a drop down list. On changing the index of the dropdownlist , I populate an asp.net gridview.
I have a requirement where the user should be able to remove individual rows of the gridview on the screen .
At the end of each row, I intend to have a remove button. On clicking the button the row should
disappear . But this should be only on the screen. There should be no changes done in the database.
I have my code below right now :
aspx
<table>
<tr>
<td>
<div>
<asp:Label ID="lblClient" runat="server" Text="Client :" CssClass="label" ForeColor="Black"></asp:Label>
<asp:DropDownList ID="ddlClient" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlClient_SelectedIndexChanged">
<asp:ListItem Text="ALL" Value="0"></asp:ListItem>
</asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvMainLog" runat="server" Visible="true" AllowSorting="True" AutoGenerateColumns="False"AllowPaging="true">
<Columns>
<asp:BoundField DataField="Instruction" HeaderText="Instruction" />
<asp:BoundField DataField="ProviderId" HeaderText="Id" />
</Columns>
</asp:GridView>
<div>
<asp:TextBox ID="txtEditMin" runat="server"></asp:TextBox>
</div>
</td>
</tr>
</table>
aspx.cs
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
gvMainLog.DataSource = GetSetupUtility(1);
gvMainLog.DataBind();
}
In the GridView add the remove command like this
<Columns>
<asp:BoundField DataField="Instruction" HeaderText="Instruction" />
<asp:BoundField DataField="ProviderId" HeaderText="Id" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Remove"
Text="Remove" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
In the action of the remove button use the DeleteRow method
void gvMainLog_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
var id = Int32.Parse(e.CommandArgument);
gvMainLog.DeleteRow(id);
}
}
You need somthing like this:
Use TemplateField in gridview.
<script>
function deleteRow(rowId) {
$("#" + rowId).remove();
}
</script>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div id='<%# "myRow" + Container.DataItemIndex %>'> contents <img src="deleteImageUrl" onclick='<%# "deleteRow(\"myRow" + Container.DataItemIndex+"\")" %>'/> </div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

How can i have a gridview or details view in a popup

This is my design
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Arial"
Font-Size="11pt" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
CellPadding="4" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckAll" onclick="return check_uncheck (this );" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="ID" Visible="false" Text='<%# DataBinder.Eval (Container.DataItem, "Id") %>'
runat="server" />
<asp:CheckBox ID="deleteRec" onclick="return check_uncheck (this );" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FileName" SortExpression="FileName">
<EditItemTemplate>
<asp:Label ID="LblFileName" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblFileName1" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
<asp:ImageButton ID="img" runat="Server" CommandName="Image" ImageUrl="~/Images/pen.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<RowStyle BackColor="#F7F7DE" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:Panel ID="pnlAddEdit" runat="server" CssClass="modalPopup" Style="display: none"
Width="1000px">
<asp:Label Font-Bold="true" ID="Label4" runat="server" Text="File Data"></asp:Label>
<br />
<table align="center" width="1000px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="RecordTypeCode"></asp:Label>
</td>
<td>
<asp:Label ID="lblRec" runat="server" Text="Content"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblFileHeader" runat="server" Text="FileHeader"></asp:Label>
</td>
<td>
<asp:Label ID="txtCustomerID" Width="500px" MaxLength="5" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblBatchHeader" runat="server" Text="BatchHeader"></asp:Label>
</td>
<td>
<asp:Label ID="txtBatch" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="EntryDetail"></asp:Label>
</td>
<td>
<asp:Label ID="txtEntry" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" />
</td>
<td>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="return Hidepopup()" />
</td>
</tr>
</table>
</asp:Panel>
<asp:LinkButton ID="lnkFake" runat="server"></asp:LinkButton>
<cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="false" PopupControlID="pnlAddEdit"
TargetControlID="lnkFake" BackgroundCssClass="modalBackground">
</cc1:ModalPopupExtender>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
<asp:AsyncPostBackTrigger ControlID="btnSave" />
</Triggers>
</asp:UpdatePanel>
I have taken some text boxes inside a panel when a pop-up opens but instead of that i need a gridview to be there as i have to display the content of a large file
This is my data
101 111100022 5104885671104200936A094101CapitalOne MudiamInc
5220MudiamInc A510488567CCDITServices000000110422 1111100020000001
622968765348545646565 00004000001007 rajeshk 1111100020000001
62297877654775676546546 00002888891007 rajeshk 1111100020000002
82200000020194754188000000000000000000688889A510488567 111100020000001
5220MudiamInc A510488567CCDITServices000000110422 1111100020000002
62212345678034354465677 00000864451005 swethau 1111100020000003
62212345678087664534543 00000559841011 swathiK 1111100020000004
62212345678097867546435 00000579351012 lavanyaK 1111100020000005
6221234567806754654435435 00000846761013 AnithaN 1111100020000006
82200000040049382712000000000000000000285040A510488567 111100020000002
9000002000001000000060244136900000000000000000071739300
This i have to show sequentially in an order as
RecordTyecode Content
FileHeader Starting line to be here
BatchHeader Line that come's with 5(First come line)
EntryDetail Number of 6 line has to be added on by one
BatchControl line that starts with 8 has to be here
Again if i have line starts with 5 after 8 that has to be appended like above sequence
Can any give me an idea to add this dynamically a little tricky to understand ask if any information required
This was done in winforms exact i need in web too
Just in a simple way i get this answer
Declared a hash table
static Hashtable rectype = new Hashtable();
In page load i add the following
if (rectype != null)
{
rectype.Add("1", "File Header");
rectype.Add("5", "Batch Header");
rectype.Add("6", "Entry Detail");
rectype.Add("7", "Addenda Record");
rectype.Add("8", "Batch Control");
rectype.Add("9", "File Control");
}
Here is my code when user clicks on Image button
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = 0;
string strLine = string.Empty;
string[] lines = null;
if (e.CommandName == "Image")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
Label l = (Label)GridView1.Rows[row.RowIndex].Cells[1].FindControl("Label1");
id = Convert.ToInt16(l.Text);
string selectSQL = "Select File_Data from tblachmaster WHERE Id IN (" + id + ")";
MySqlCommand cmd1 = new MySqlCommand(selectSQL);
cmd1.Parameters.Add("#_id", SqlDbType.Int).Value = id;
DataTable dt1 = GetData1(cmd1);
if (dt1 != null)
{
for (int i = 0; i < dt1.Rows.Count; i++)
{
Byte[] bytes = (Byte[])dt1.Rows[i]["File_Data"];
string text = Encoding.UTF8.GetString(bytes);
lines = Regex.Split(text, "\r\n");
strLine = convertArrayToString(lines);
}
}
DataTable table = new DataTable();
table.Columns.Add("RecordTypeCode", typeof(string));
table.Columns.Add("Content", typeof(string));
foreach (string strcontent in lines)
{
if (strcontent != string.Empty)
table.Rows.Add(rectype[(strcontent.Substring(0, 1))], strcontent);
}
dynamicGridView.DataSource = table;
dynamicGridView.DataBind();
popup.Show();
}
}
This works well for me and here is my output after clicking on Image button
You can use Ajax pop up control similar to this -
http://www.ezzylearning.com/tutorial.aspx?tid=2861497

Categories