Get current GridView - asp.net

I've got a page with 3 GridViews.
<div id="Export" runat="server" class="headeropt noprint">
<asp:LinkButton runat="server" ID="buttonexport" OnClick="buttonexport_Click" OnClientClick="Javascript:Noshow();" CssClass="btn btn-default">
<asp:Image runat="server" ImageUrl="/images/Excel-16.gif" />
<asp:Label runat="server" Text="Export" />
</asp:LinkButton>
</div>
<div class="col-md-12">
<asp:GridView ID="grid1" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid1_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid1_RowEditing" datakeynames="grid1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("name") %>' Text='<%# Eval("name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("company") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid2" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid2_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid2_RowEditing" datakeynames="grid2">
<Columns>
<asp:TemplateField HeaderText="Product Family">
<ItemTemplate>
<asp:LinkButton ID="lnkprdfamily" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("prdfamily") %>' Text='<%# Eval("prdfamily") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid3" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid3_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" datakeynames="grid3">
<Columns >
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
as you can see, the first grid links to the second, and the second to the third.
What I want to know, is at any given time to get the grid I'm viewing at the moment.
Just edited adding the linkButton that exports the grid to excel. This is why I need the gridview that I'm currently on, so that the export command works at any time.
Here's the export code:
protected void buttonexport_Click(object sender, EventArgs c)
{
Master.Page.Form.Attributes.Remove("onsubmit");
CB.ChangeControlsToValue(grid1);
string attachment = "attachment; filename=File.xls";
//Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Thanks.

Nevemind, got a solution.
I've just checked the one that is visible at the moment and work with it.
bool list = grid1.Visible;
if (list)
{ CB.ChangeControlsToValue(grid1); }
and so on....

Related

export gridview to excel is not working with Ajax and Update panel

I have a grid-view in Update panel. there is a link button inside grid-view which fire export command in row command event. There is an another grid-view which get exported on this command code. I debug, the pointers goes in but the export not happening. I know i am missing something here, but not able to get it after spending lot of hours. The Script manager is on master page.
Can any one help please. Below is the code.
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_Report" runat="server" ShowFooter="true" CssClass="ui-jqgrid-btable table-hover" AutoGenerateColumns="false" AllowPaging ="true" PageSize ="12" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_CampaignName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreateDate">
<ItemTemplate>
<asp:Label ID="lbl_CreateDate" runat="server" Text='<%#Eval("CreateDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="130px" />
<HeaderStyle Width="130px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="sender">
<ItemTemplate>
<asp:Label ID="lbl_senderID" runat="server" Text='<%#Eval("sender") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="85px" />
<HeaderStyle Width="85px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList class="ui-pg-selbox" ID="drp_itemCount" runat="server" AutoPostBack="true"
onselectedindexchanged="drp_itemCount_SelectedIndexChanged" >
<asp:listItem>10</asp:listItem>
<asp:listItem>15</asp:listItem>
<asp:listItem>50</asp:listItem>
<asp:listItem>100</asp:listItem>
<asp:listItem>200</asp:listItem>
</asp:DropDownList>
</FooterTemplate>
<ItemStyle Width="375px" />
<HeaderStyle Width="375px" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="lbl_SMSCount" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="80px" />
<HeaderStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Count">
<ItemTemplate>
<table class="deliveryCount">
<tr>
<td style="padding:5px;width:180px" colspan="2">Total: <%#Eval("Total")%></td>
</tr>
<table style="font-size:11px;width:180px;">
<tr>
<td style="padding:5px;font-size:11px;width:80px">A: <%#Eval("A")%></td>
<td style="padding:5px;font-size:11px;width:80px">B: <%#Eval("B")%></td>
</tr
<tr>
<td style="padding:5px;font-size:11px;width:80px">2: <%#Eval("2")%></td>
<td style="padding:5px;font-size:11px;width:80px">1: <%#Eval("1")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk_download" style="font-size:11px;" CommandName="download" CommandArgument='<%#Eval("CampaignId") %>' text="Download" class="ui-icon-document"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Size="13" Font-Bold="true" />
<RowStyle Font-Names="Helvetica" Font-Size="10" />
<PagerStyle CssClass ="gridviewPager" Height="30px" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="grd_export" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel6" runat="Server">
<ContentTemplate>
<asp:GridView ID="grd_export" runat="server" Visible="false" AutoGenerateColumns ="false" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number">
<ItemTemplate>
<asp:Label ID="lbl_Number" runat="server" Text='<%#Eval("Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lbl_Id" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="text Id">
<ItemTemplate>
<asp:Label ID="lbl_MessageId" runat="server" Text='<%#Eval("textId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lbl_Date" runat="server" Text='<%#Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lbl_Status" runat="server" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credits">
<ItemTemplate>
<asp:Label ID="lbl_Credits" runat="server" Text='<%#Eval("Credits") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Below is the code on Row Command event
If e.CommandName = "download" Then
Dim str As String
str = " select * FROM [Box] where MsgId='" + e.CommandArgument + "'"
Dim com As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(com)
Dim dt As New DataTable
da.Fill(dt)
If Not dt Is Nothing Then
If dt.Rows.Count > 0 Then
grd_export.DataSource = dt
grd_export.DataBind()
grd_export.Visible = True
Response.Clear()
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.AddHeader("content-disposition", "attachment;filename=Message.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
Me.EnableViewState = False
Dim stringwrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlwrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringwrite)
grd_export.RenderControl(htmlwrite)
Response.Write(stringwrite.ToString())
grd_export.Visible = False
Response.End()
End If
End If
End If
You need to add a postback trigger to your update panel for respose.write to work.

Failed to load viewstate - dynamic controls in ASP.NET using nested GridView

In my project I am working on showing Task Group and Task (i.e. Parent-child) in one screen with the ability to Add/Edit/Delete the parent (Task Group) record and also to Add/Edit/Delete associated Task. For this I am using nested Grid View (parent-child).
I am implementing the sample application mentioned in the below link to my ASP.NET web based project.
http://www.codeproject.com/Articles/20047/Editable-Nested-GridView-All-in-One
my .ASPX looks as mentioned below:
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Unicorn.Master" CodeBehind="TaskAndTaskGroup.aspx.cs" Inherits="Vipassi.Web.TaskGroup.TaskAndTaskGroup"%>
<asp:Content ID="Content1" ContentPlaceHolderID="phContent" runat="server">
<script language="javascript" type="text/javascript">
function expandcollapse(obj, row) {
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
if (div.style.display == "none") {
div.style.display = "block";
if (row == 'alt') {
img.src = "../../Resources/images/minus.gif";
}
else {
img.src = "../../Resources/images/minus.gif";
}
img.alt = "Close to view other TaskGroups";
}
else {
div.style.display = "none";
if (row == 'alt') {
img.src = "../../Resources/images/plus.gif";
}
else {
img.src = "../../Resources/images/plus.gif";
}
img.alt = "Expand to show Tasks";
}
}
</script>
<div>
<asp:GridView ID="GridView1" AllowPaging="True" BackColor="#f1f1f1"
AutoGenerateColumns="false" DataSourceID="ObjectDataSource1" DataKeyNames="task_group_id"
ShowFooter="true" Font-Size="Small"
Font-Names="Verdana" runat="server" GridLines="None" OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" BorderStyle=Outset
OnRowDeleting="GridView1_RowDeleting" OnRowDeleted="GridView1_RowDeleted"
OnRowUpdated="GridView1_RowUpdated" AllowSorting="true">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:expandcollapse('div<%# Eval("task_group_id") %>', 'one');">
<img id="imgdiv<%# Eval("task_group_id") %>" alt="Click to show/hide Tasks for TaskGroup <%# Eval("task_group_id") %>" width="9px" border="0" src="../../Resources/images/plus.gif" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TaskGroup ID" SortExpression="task_group_id">
<ItemTemplate>
<asp:Label ID="lblTaskGroupID" Text='<%# Eval("task_group_id") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTaskGroupID" Text='<%# Eval("task_group_id") %>' runat="server"></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskGroupID" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Group" SortExpression="task_group">
<ItemTemplate><%# Eval("task_group") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskGroup" Text='<%# Eval("task_group") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskGroup" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fiscal Year" SortExpression="Fiscal_Year">
<ItemTemplate><%# Eval("Fiscal_Year") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFiscalYear" Text='<%# Eval("Fiscal_Year") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFiscalYear" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="Active_Flag">
<ItemTemplate><%# Eval("Active_Flag")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtActiveFlag" Text='<%# Eval("Active_Flag") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtActiveFlag" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaskGroup" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTaskGroup" CommandName="AddTaskGroup" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("task_group_id") %>" style="display: none; position: relative; left: 15px; overflow: auto; width: 97%">
<asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" BackColor="White" Width="100%" Font-Size="X-Small"
AutoGenerateColumns="false" Font-Names="Verdana" runat="server" DataKeyNames="task_group_id" ShowFooter="true"
OnPageIndexChanging="GridView2_PageIndexChanging" OnRowUpdating="GridView2_RowUpdating"
OnRowCommand="GridView2_RowCommand" OnRowEditing="GridView2_RowEditing" GridLines="None"
OnRowUpdated="GridView2_RowUpdated" OnRowCancelingEdit="GridView2_CancelingEdit" OnRowDataBound="GridView2_RowDataBound"
OnRowDeleting="GridView2_RowDeleting" OnRowDeleted="GridView2_RowDeleted" OnSorting="GridView2_Sorting"
BorderStyle="Double" BorderColor="#0083C1">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Task ID" SortExpression="task_id">
<ItemTemplate>
<asp:Label ID="lblTaskID" Text='<%# Eval("task_id") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTaskID" Text='<%# Eval("task_id") %>' runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task" SortExpression="task_name">
<ItemTemplate><%# Eval("task_name")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTask" Text='<%# Eval("task_name")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTask" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fiscal Year" SortExpression="fiscal_year">
<ItemTemplate><%# Eval("fiscal_year")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskFiscalYear" Text='<%# Eval("fiscal_year")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskFiscalYear" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="Active_Flag">
<ItemTemplate><%# Eval("Active_Flag")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskActiveFlag" Text='<%# Eval("Active_Flag")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskActiveFlag" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaskGroup" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTask" CommandName="AddTask" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource SortParameterName="sortExpression" EnablePaging="true" ID="ObjectDataSource1"
MaximumRowsParameterName="maxRecords" TypeName="Vipassi.BLL.Classes.TaskGroup.TaskGroupBLL"
SelectMethod="ListforTaskGroup" SelectCountMethod="ListCountforTaskGroup" StartRowIndexParameterName="startIndex"
runat="server">
<SelectParameters>
<asp:SessionParameter SessionField="orgId" Name="orgId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
Option 1:
You can disable the VIEWSTATE of ASPX page /User Control :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="~/TaskAndTaskGroup.aspx.cs" Inherits="Vipassi.Web.TaskGroup.TaskAndTaskGroup" EnableViewState="false"%>
Option 2:
You can also change your source code to make sure the controls added during a post-back must match the type and position of the controls added during the initial request.
Hope this will help you.

Gridview edit not working

I have the following gridview:
<asp:GridView ID="GrdAll" runat="server" AutoGenerateColumns="false" ShowHeader="false"
Width="40%" GridLines="None" CellPadding="2" CellSpacing="2">
<AlternatingRowStyle CssClass="AltColor22" />
<RowStyle CssClass="AltColor21" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" class="TableBorder">
<tr>
<td valign="top" align="left">
<asp:Label ID="lblName" runat="server" />
<hr />
</td>
</tr>
<tr>
<td valign="top" align="left" class="TableBorder">
<asp:GridView ID="GrdContent" runat="server" AutoGenerateColumns="False"
Width="100%" GridLines="None" CellPadding="2" CellSpacing="2"
OnRowDataBound="GrdContent_RowDataBound" OnRowEditing="GrdContent_RowEditing"><%----%>
<Columns>
<asp:TemplateField HeaderText="Kiosk ID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblKiosID" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtKiosk" runat="server" Text ='<%#Eval("HBEmailID") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email ID" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text ='<%#Eval("HBEmailID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit"
Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this, edit functionality is not working.
In clicking of edit button, it shows me nothing. (It should show me textbox to edit)
Following is code:
Protected Sub GrdContent_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
Dim row As GridViewRow = DirectCast(TryCast(sender, Control).Parent.Parent, GridViewRow)
Dim index As Integer = row.RowIndex
CType(row.FindControl("GrdContent"), GridView).EditIndex = e.NewEditIndex
DBName = CType(row.FindControl("lblName"), Label).Text
bindContentGrid(CType(row.FindControl("GrdContent"), GridView), DBName)
End Sub
Please help me, it does not gives me error, but it also not showing me textbox to edit.
The issue could be on the following code
bindContentGrid(CType(row.FindControl("GrdContent"), GridView), DBName)
If that code does not return any records, then nothing will be displayed. Make sure it is returning some rows.

ASP.NET Dynamically Adding a Gridview

I have a gridview as such
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." AlternatingRowStyle-CssClass="tr_dark" HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" AllowSorting="false" Width="100%" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product" >
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("description").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize" >
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server" Text='<%# Eval("packSize").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
<ItemTemplate>
<asp:Label ID="StockField" runat="server" Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity" >
<ItemTemplate>
<asp:TextBox runat="server" Width="30" ID="txtQuantity" Text='<%# Eval("Quantity") %>'></asp:TextBox>
<asp:TextBox runat="server" Visible="false" Width="30" ID="txtProductCode" Text='<%# Eval("ProductCode") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server" Text='<%# DisplayMoney(Eval("UnitPrice").ToString())%>'></asp:Label>
<asp:Label ID="TradePriceFieldHidden" runat="server" Text='<%# Eval("UnitPrice").ToString()%>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:BoundField DataField="TotalPrice" HeaderText="Total" HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />
</Columns>
<Columns>
<asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/img/icons/cross.gif" width="10" height="10" alt="Cancel" runat="server" ID="btnRemove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
And now trying to add a footer rows dynamically ( because I need multiple footer rows)
Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound
' If we are binding the footer row, let's add in our total
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(5).Text = "<strong>Total Cost:</strong>"
e.Row.Cells(6).Text = ShoppingCart.Instance.GetSubTotal().ToString("C")
End If
Dim grid As GridView = CType(sender, GridView)
''gets the current footer row to clone
Dim footer As GridViewRow = grid.FooterRow
Dim numCells = footer.Cells.Count
Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)
''have to add in the right number of cells
''this also copies any styles over from the original footer
For i As Integer = 0 To numCells - 1
Dim emptyCell As New TableCell
emptyCell.ApplyStyle(grid.Columns(i).ItemStyle)
newRow.Cells.Add(emptyCell)
Next
newRow.Cells(5).Text = "Total Discount:"
newRow.Cells(6).Text = "55.00"
''add new row to the gridview table, at the very bottom
CType(grid.Controls(0), Table).Rows.Add(newRow)
End Sub
But getting the error
Object reference not set to an instance of an object.
Dim numCells = footer.Cells.Count
Any idea whats going wrong ?
The GridView FooterRow property is only set after the RowDataBound method has been called. You can access it in the DataBound method of the GridView.
Protected Sub gvShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim footerRow as GridViewRow = gvShoppingCart.FooterRow
End Sub
First of all your GridView Markup is incorrect. Define Columns just once like this.
<asp:GridView ID="gvShoppingCart" runat="server"
AlternatingRowStyle-CssClass="tr_dark"
AllowPaging="true"
AllowSorting="false"
AutoGenerateColumns="false"
BorderWidth="0px"
DataKeyNames="ProductId"
EmptyDataText="There is nothing in your shopping cart."
GridLines="None"
HeaderStyle-CssClass="header_req"
PageSize="25"
ShowFooter="true"
Width="100%"
OnRowCommand="gvShoppingCart_RowCommand"
OnRowDataBound="gvShoppingCart_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="productName" HeaderStyle-CssClass="product">
<ItemTemplate>
<asp:Label ID="ProductNameField" runat="server"
Text='<%# Eval("description").ToString() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pack Size" SortExpression="packSize" HeaderStyle-CssClass="packsize">
<ItemTemplate>
<asp:Label ID="PackSizeField" runat="server"
Text='<%# Eval("packSize").ToString()%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Stock" SortExpression="address" HeaderStyle-CssClass="stock">
<ItemTemplate>
<asp:Label ID="StockField" runat="server"
Text='<%# DisplayStockLevel(Eval("StockIndicator").ToString()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" HeaderStyle-CssClass="quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server"
Width="30"
Text='<%# Eval("Quantity") %>'>
</asp:TextBox>
<asp:TextBox ID="txtProductCode" runat="server"
Visible="false"
Width="30"
Text='<%# Eval("ProductCode") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Price" HeaderStyle-CssClass="actual" SortExpression="address">
<ItemTemplate>
<asp:Label ID="TradePriceField" runat="server"
Text='<%# DisplayMoney(Eval("UnitPrice").ToString())%>'>
</asp:Label>
<asp:Label ID="TradePriceFieldHidden" runat="server"
Text='<%# Eval("UnitPrice").ToString()%>'
Visible="false">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TotalPrice" HeaderText="Total"
HeaderStyle-CssClass="total" HeaderStyle-HorizontalAlign="Right"
DataFormatString="{0:C}" />
<asp:TemplateField HeaderText="" HeaderStyle-CssClass="remove">
<ItemTemplate>
<asp:ImageButton ID="btnRemove" runat="server"
ImageUrl="~/img/icons/cross.gif"
Width="10"
Height="10"
AlternateText="Cancel"
CommandName="Remove"
CommandArgument='<%# Eval("ProductId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Secondly, you are getting error because you have not defined your FooterTemplate anywhere.

Display text in multiline inside a grid view in asp.net

I am using a grid view in my asp.net application. In one column i need to display description(minimum 5 characters. Maximum 255 characters).i am using a label to hold description in that grid view.
But my problem is that if the description is larger it stretches in the browser and show it in one line. I want to display description in multi line (like a paragraph)
I hope some one help me . the entire grid view code is shown below
<asp:GridView ID="gv_View_Documents" runat="server" AllowSorting="true" DataKeyNames="DocumentName,Description" SkinID="customGridview" AutoGenerateColumns="false" OnSorting="gv_View_Documents_Sorting" OnRowCancelingEdit="gv_View_Documents_RowCancelingEdit" OnRowCommand="gv_View_Documents_RowCommand"
OnRowEditing="gv_View_Documents_RowEditing" OnRowUpdating="gv_View_Documents_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Document Name" HeaderStyle-Width="200" HeaderStyle-CssClass="GridHeaderStyle" SortExpression="DocumentName" >
<ItemTemplate>
<asp:LinkButton CommandName="ViewDocument" CssClass="GridHeaderStyle" ID="hlnk_View_Document" runat="server" CommandArgument='<%# Bind("DocumentName") %>' Text='<%# Bind("DocumentName") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="200" HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lbl_gv_DocumentDescription" runat="server" Text='<%# Bind("Description") %>' ></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_EditDescription" MaxLength="250" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="50" HeaderStyle-CssClass="GridHeaderStyle" ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="Bttn_Update_Description" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="Bttn_Cancel_Settings" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton></EditItemTemplate><ItemTemplate>
<asp:LinkButton ID="Bttn_Edit_Description" ForeColor=" #555555" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton></ItemTemplate><ControlStyle CssClass="edit" />
</asp:TemplateField>
</Columns>
</asp:GridView>
try this... working properly...for wrapping text in Gridview
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;width:100px");
}
}
}
Sometimes ItemStyle Wrap="true" doesn't work. To be certain your text wraps, surround the Label in a and set a width on the surrounding div.
EDIT
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div style="width:100px;">
<asp:Label ID="Label2" runat="server" Text="<%# Eval("Name") %>"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="<%# Eval("Age") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can set the ItemStyle of the TemplateField to true like this:
<ItemStyle Wrap="true" Width="100px" />
Complete gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Wrap="true" Width="100px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Screenshot:
Try like this by applying the css class
.paraGraphtext
{
white-space: pre-wrap;
}
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label>
</ItemTemplate>

Resources