How to add footer to gridview - asp.net

I have a grid with 3 columns - Edit, ID, Movie. I would like to add a footer with an Insert link button, 2 textboxes respectively, but unable to do so. Is it possible.
ASPX:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"
OnRowEditing="gridview1_RowEditing"
OnRowCancelingEdit="gridview1_RowCancelingEdit"
ShowFooter="true" >
<Columns>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="movie" HeaderText="MOVIE" />
</Columns>
</asp:GridView>
When I try the following, there is an error for commandfield which says, this element is not supported.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</columns>
The other way would be to use itemTemplate & EditTemplate for each column control. But I find this simple and would like to proceed this way. So Can I add a footer to this structure.

YES, It is possible. But this will require using the <FooterTemplate> inside <TemplateField>. Use TemplateFields for each of the columns and also set the FooterTemplate for each of the columns.
NOTE: The ID column seems here to be a Primary Key. So remove the <FooterTemplate> from the corresponding <TemplateField> defined for ID column, if ID is a Primary Key OR an autogenerated field in your database.
NOTE II: The <FooterTemplate> simply will contain a TextBox only.
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnCancel" runat="server"
CausesValidation="False"
CommandName="Cancel" Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkBtnInsert" runat="server"
CommandName="Insert">Insert</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnDelete" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<asp:TextBox ID="TextBoxID" runat="server" Text='<%# Bind("ID") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MOVIE">
<EditItemTemplate>
<asp:TextBox ID="TextBoxMovie" runat="server" Text='<%# Bind("Movie") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Movie")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now there are 2 ways to insert Data. Either you can use GridView OnRowCommand event or you can handle the OnClick event of your Insert button.

You can't place a commandfield inside TemplateField. But can do like this:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="False"
OnRowEditing="gridview1_RowEditing"
OnRowCancelingEdit="gridview1_RowCancelingEdit"
ShowFooter="true" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<!--To fire the OnRowEditing event.-->
<asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<!--To fire the OnRowDeleting event.-->
<asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete"
Text="Delete">
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" Text="Insert"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="movie" HeaderText="MOVIE" />
</Columns>
</asp:GridView>

Related

How to use 2 gridview cell values column to build a hyper link

I have a gridview. I need to build a hyperlink in one column with the value from a different one and its own value. So for example the URL would be something.com/column1value/column2value.
I have the System and theregion hyperlinked via a TemplateField in there respective column. It is in column 2 that i want to build the url based on the value in column1
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SQL" Class='table table-hover' BorderStyle="None" BorderColor="White" GridLines="None" UseAccessibleHeader="False" ShowFooter="True" ShowHeaderWhenEmpty="True" EnableTheming="False">
<Columns>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:TemplateField HeaderText="System" SortExpression="System">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("System") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("System", "http://evemaps.dotlan.net/system/{0}/") %>' Target="_blank" Text='<%# Eval("System", "{0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Region" SortExpression="Region">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Region") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Region", "http://evemaps.dotlan.net/Map/{0}/") %>' Target="_blank" Text='<%# Eval("Region", "{0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" />
<asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
<asp:TemplateField HeaderText="EvE Time" SortExpression="EvE Time">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("[EvE Time]") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="EvETimeLB" runat="server" Text='<%# Bind("[EvE Time]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Font-Bold="True" />
</asp:GridView>
Using Eval is not restricted to the column you are using it in. You can use all the columns in an ItemTemplate.
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank"
NavigateUrl='<%# string.Format("something.com/{0}/{1}", Eval("column1"), Eval("column2")) %>'
Text='<%# string.Format("something.com/{0}/{1}", Eval("column1"), Eval("column2")) %>'>
</asp:HyperLink>

Gridview Not Triggering Events

I'm trying to use the events on my Gridview control. But its not firing the events when expected. i.e, for OnSelectedIndexChanged, when changing the selected cell/row nothing happens. I've tried with OnSelectedIndexChanged as well as OnRowUpdating. The breakpoint is never hit in gvQ15_RowUpdating. I've even tried using the OnTextChanged event for the child textboxes or the gridview and they are not firing.
Markup
<asp:GridView AutoPostBack="true" ID="gvQ15" runat="server" AllowPaging="false" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Prescription Medication Name" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox ID="txtPrescriptionMedicationName" runat="server" Width="100%" OnTextChanged="txtPrescriptionMedicationName_TextChanged" BordewWidth="0" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Originally Prescribed" ItemStyle-Width="10%">
<ItemTemplate>
<asp:TextBox ID="txtDateOriginallyPrescribed" runat="server" Style="min-width: 110px; width: 100%;" BorderWidth="0" OnTextChanged="txtDateOriginallyPrescribed_TextChanged" />
<asp:MaskedEditExtender ID="meetxtDateOriginallyPrescribed" runat="server" MaskType="date" UserDateFormat="MonthDayYear" Mask="99/99/9999" TargetControlID="txtDateOriginallyPrescribed" ClearMaskOnLostFocus="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Frequency and Dosage" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox ID="txtFrequencyAndDosage" runat="server" Width="100%" BorderWidth="0" OnTextChanged="txtFrequencyAndDosage_TextChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Condition" ItemStyle-Width="25%">
<ItemTemplate>
<asp:TextBox ID="txtCondition" runat="server" Width="100%" BorderWidth="0" OnTextChanged="txtCondition_TextChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Onset Date" ItemStyle-Width="10%">
<ItemTemplate>
<asp:TextBox ID="txtConditionOnsetDate" runat="server" style="min-width:110px;width:100%" BorderWidth="0" OnTextChanged="txtConditionOnsetDate_TextChanged" />
<asp:MaskedEditExtender ID="meeConditionOnsetDate" runat="server" MaskType="date" UserDateFormat="MonthDayYear" Mask="99/99/9999" TargetControlID="txtConditionOnsetDate" ClearMaskOnLostFocus="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind
Protected Sub gvQ15_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Session("Prescriptions") = gvQ15.DataSource
End Sub
Add the CausesValidation="False" attribute to your CommandField.
<asp:commandfield ShowEditButton="True" CausesValidation="False" HeaderText="Edit"/>
Or,
Add the EnableViewState="True" attribute to your GridView.
For TemplateField use this:
<asp:TemplateField HeaderText="Command">
<ItemTemplate>
<asp:Button CommandName="Edit" Text="Edit" ID="btnEdit" Runat="server"></asp:Button>
<asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button CommandName="Update" Text="Update" ID="btnUpdate" Runat="server"></asp:Button>
<asp:Button CommandName="Cancel" Text="Delete" ID="btnCancel" Runat="server"></asp:Button>
</EditItemTemplate>
</asp:TemplateField>

FormView Layout

This has driven me crazy a few times in the past but I've always fudged it and moved on. Today I want to try and fix it!
I quite frequently create a GridView to display data and a FormView directly beneath in Insert mode with controls in the InsertItemTemplate corresponding to the columns in the GridView, so that it looks like the FormView is just an empty row of the GridView awaiting new data entry. This works great.
The problem is that I can never get the widths of the 'columns' (really just textboxes) in the FormView to correspond to the widths of the columns in the GridView above.
In the example below, as you can see, both the GridView and FormView have a width of 100% and, sure enough, when the page is rendered they're both exactly the same width (I gave the FormView a border briefly to check). However, even though the widths of the textboxes in the FormView are identical to the widths of the columns in the GridView, they don't display that way. The textboxes are slightly wider, and by the time you get to the right-most column the cumulative effect means that the alignment is way out.
I'm guessing the problem is something to do with border widths or some other hidden element which is being rendered, but I can't figure it out. I have to say, the amount by which the alignment is out seems more than the couple of pixels a border would affect things.
<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="false" DataMember="DefaultView" DataKeyNames="TPRID" Width="100%" >
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:BoundField DataField="TPREnteredAt" HeaderText="Entered At" ReadOnly="True" SortExpression="TPREnteredAt" ItemStyle-Width="24%" ControlStyle-Width="90%" />
<asp:BoundField DataField="TPRTemp" HeaderText="Temp" ReadOnly="True" SortExpression="TPRTemp" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPulse" HeaderText="Pulse" ReadOnly="True" SortExpression="TPRPulse" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRRespiration" HeaderText="Respiration" ReadOnly="True" SortExpression="TPRRespiration" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRPCV" HeaderText="PCV" ItemStyle-Width="12%" ControlStyle-Width="90%"/>
<asp:BoundField DataField="TPRTP" HeaderText="TP" ItemStyle-Width="12%" ControlStyle-Width="90%" />
<asp:CommandField ButtonType="Button" InsertVisible="False" ShowEditButton="True" ItemStyle-Width="16%" UpdateText="Save" ControlStyle-Width="60px" />
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
<asp:FormView ID="fvTPR" runat="server" DataSourceID="SQLTPR" DefaultMode="Insert" Width="100%" >
<InsertItemTemplate>
<asp:textbox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
<asp:Button ID="btnAddTPR" runat="server" Text="Save" Width="5%" />
</InsertItemTemplate>
</asp:FormView>
This renders like this:
How about using GridView footer for add funcionality? This way you will have all columns in same table and you'l get around your positioning problems.
Here is an example of how your gridview should look like:
<asp:GridView ID="gvTPR" runat="server" DataSourceID="SQLTPR" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="TPRID" Width="100%" EnableModelValidation="True">
<RowStyle HorizontalAlign="Center" />
<EditRowStyle BackColor="#ccffff" />
<HeaderStyle BackColor="#013b82" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Entered At" SortExpression="TPREnteredAt">
<EditItemTemplate>
<asp:TextBox ID="lblEnteredAt" runat="server" Text='<%# Eval("TPREnteredAt") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TPREnteredAt") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="24%" />
<FooterTemplate>
<asp:TextBox ID="lblEnteredAt" runat="server" Text="Will be added automatically" Width="24%" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Temp" SortExpression="TPRTemp">
<EditItemTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Eval("TPRTemp") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("TPRTemp") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTemp" runat="server" Text='<%# Bind("TPRTemp")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Pulse" SortExpression="TPRPulse">
<EditItemTemplate>
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Eval("TPRPulse") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("TPRPulse") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPulse" runat="server" Text='<%# Bind("TPRPulse")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Respiration" SortExpression="TPRRespiration">
<EditItemTemplate>
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Eval("TPRRespiration") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("TPRRespiration") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRespiration" runat="server" Text='<%# Bind("TPRRespiration")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="PCV">
<EditItemTemplate>
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("TPRPCV") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPCV" runat="server" Text='<%# Bind("TPRPCV")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TP">
<EditItemTemplate>
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("TPRTP") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTP" runat="server" Text='<%# Bind("TPRTP")%>' Width="12%" />
</FooterTemplate>
<ControlStyle Width="90%" />
<ItemStyle Width="12%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="lnkbtnEdit" runat="Server" Text="Edit" CommandName="Edit"
CausesValidation="false"></asp:Button>
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="lnkbtnUpdate" runat="Server" Text="Save" CommandName="Update"
CausesValidation="true"></asp:Button><br />
<asp:LinkButton ID="lnkbtnCancel" runat="Server" Text="Cancel" CommandName="Cancel"
CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkbtnInsert" runat="Server" Text="Save" CommandName="Insert"
CausesValidation="true"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No TPR records exist
</EmptyDataTemplate>
</asp:GridView>
Hope this helps!
Regards,
Uros

Gridview row editing and deleting

I have a grid to update the employee's confirmation. I need to change the data from grid using edit and delete link. I have trouble to create this grid. Because gridview all textbox enable always.Textbox only enable when am click the edit button.
This is my partial code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
Width="100%" AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None"
ShowHeaderWhenEmpty="True" EmptyDataText="No Data Found"
onpageindexchanging="GridView1_PageIndexChanging"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="EMP ID">
<ItemTemplate>
<asp:TextBox ID="lblempId" runat="server" Text='<%#Bind("fldemp_id") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPLOYEE NAME">
<ItemTemplate>
<asp:TextBox ID="lblusername" runat="server" Text='<%#Bind("fldempname") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JOINDATE">
<ItemTemplate>
<asp:TextBox ID="lbljoin" runat="server" Text='<%#Bind("fldjoindate") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CONFIRMATION DATE">
<ItemTemplate>
<asp:TextBox ID="lblconfirm" runat="server" Text='<%#Bind("fldconfirmdate") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
<asp:TextBox ID="lblStatus" runat="server" Text='<%#Bind("fldstatus") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CONFIRMATION STATUS">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Eval("fldcon_status") %>' >
<asp:ListItem>Confirmed</asp:ListItem>
<asp:ListItem>Not-Confirmed</asp:ListItem>
<asp:ListItem>Extended</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/images/edit.png">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/edit.png" CommandName="edi"
CommandArgument='<%#Bind("fldemp_id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/images/Delete.png">
<ItemTemplate>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/Delete.png"
CommandName="del" CommandArgument='<%#Bind("fldemp_id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" HorizontalAlign="Left" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
If you're asking how to disable editing in the grid until someone hits an Edit button, simply set Enabled property to False on the grid until your button is pressed.

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