I have a GridView with a number of columns and it is all populating how I want it however one of the columns can have quite a bit of text in it. To try and present it nicely I'd prefer to be able to show that column in the row under it, so spanned across all columns:
As a simplified example currently I have:
Name DOB Sex Comments Title
Ian 15/04/2015 M This is MR
the text
that keeps
wrapping
I'd prefer it to be something like
Name DOB Sex Comments Title
Ian 15/04/2015 M This is MR
Comments: This is the text that keeps wrapping
Obviously this is a very simplified example but hopefully it explains what I'm trying to do. The current GridView code I have is below, I'm wanting to move intScore to the next row.
<asp:GridView ID="gvwClinical" runat="server" DataKeyNames="intClinicalAssessID" OnRowDataBound="gvwGrids_RowBound" OnRowDeleting="gvwGrids_Delete" OnSelectedIndexChanging="gvwClinical_SelectedIndexChanged" OnSorting="gvwSort" OnPageIndexChanging="gvwPage">
<Columns>
<asp:BoundField Visible="False" DataField="intClinicalAssessID"></asp:BoundField>
<asp:ButtonField Text="View" CommandName="Select" ButtonType="image"></asp:ButtonField>
<asp:ButtonField Text="Delete" CommandName="Delete" ButtonType="image"></asp:ButtonField>
<asp:BoundField DataField="dteAssessmentDate" HeaderText="Assess. Date" SortExpression="dteAssessmentDate"></asp:BoundField>
<asp:BoundField DataField="intScores" HeaderText="Scores" SortExpression="intScores" HtmlEncode="false"></asp:BoundField>
<asp:BoundField DataField="intHeight" HeaderText="Height" SortExpression="intHeight"></asp:BoundField>
<asp:BoundField DataField="intWeight" HeaderText="Weight" SortExpression="intWeight"></asp:BoundField>
<asp:BoundField DataField="strWellBeing" HeaderText="Gen Well Being" SortExpression="strWellBeing"></asp:BoundField>
<asp:BoundField DataField="strAbdominalPain" HeaderText="Abdo Pain" SortExpression="strAbdominalPain"></asp:BoundField>
<asp:BoundField DataField="strAbdominalMass" HeaderText="Abdo Mass" SortExpression="strAbdominalMass"></asp:BoundField>
<asp:BoundField DataField="strBowelFreqDay" HeaderText="Bowel Freq (day)" SortExpression="strBowelFreqDay"></asp:BoundField>
<asp:BoundField DataField="strBowelFreqNight" HeaderText="Bowel Freq (night)" SortExpression="strBowelFreqNight"></asp:BoundField>
<asp:BoundField DataField="strStoolUrgency" HeaderText="Defecation Urgency" SortExpression="strStoolUrgency"></asp:BoundField>
<asp:BoundField DataField="intLiquidStoolCount" HeaderText="Lq Stools 24 Hrs" SortExpression="intLiquidStoolCount"></asp:BoundField>
<asp:BoundField DataField="strRectalBleeding" HeaderText="Rectal Bleeding" SortExpression="strRectalBleeding"></asp:BoundField>
<asp:BoundField DataField="strEndoscopyFindings" HeaderText="Endo Findings" SortExpression="strEndoscopyFindings"></asp:BoundField>
<asp:BoundField DataField="strGlobalAssess" HeaderText="Gbl Assessment" SortExpression="strGlobalAssess"></asp:BoundField>
<asp:BoundField DataField="intActiveFistula" HeaderText="Active Fistula" SortExpression="intActiveFistula"></asp:BoundField>
<asp:BoundField DataField="intTemperature" HeaderText="Temp." SortExpression="intTemperature"></asp:BoundField>
<asp:BoundField DataField="intPulse" HeaderText="Pulse" SortExpression="intPulse"></asp:BoundField>
<asp:BoundField DataField="intHB" HeaderText="Hb" SortExpression="intHB"></asp:BoundField>
<asp:BoundField DataField="intHCT" HeaderText="Hct" SortExpression="intHCT"></asp:BoundField>
<asp:BoundField DataField="intCRP" HeaderText="CRP" SortExpression="intCRP"></asp:BoundField>
<asp:BoundField DataField="strComplications" HeaderText="Complications" SortExpression="strComplications"></asp:BoundField>
</Columns>
</asp:GridView>
You could use a TemplateField for this. This will enable you to put the longer text underneath the rest of the fields since you can design the layout per column, or row if you use only one TemplateField. you can Bind and Eval as many DataFields and html in one TemplateField as you desire if your DataSource provides them:
<Columns>
<asp:TemplateField HeaderText="Header1" SortExpression="fieldname1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("fieldname1") %>'></asp:Label>
<hr>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("fieldname2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Please note tough that this will come with side effects. You can only add a Column Header for each TemplateField you add, so ordering your data for each DataField can become tricky or pretty much impossible with the default setup from a GridView when using these fields. Since TemplateFields behave like normal BoundFields it is possible to combine BoundFields and TemplateFields in a row, but you cannot let a TemplateField show up underneath the other Columns.
As far as i know it's impossible to use BoundFields in a TemplateField, and if that's really what you are searching for maybe looking for a Pivot grid would be a better idea. But if the highly configurability and footprint of a Pivot grid suits your solution is another question.
Hope this helps in finding an awnser to your challenge. Good Luck!
Related
<asp:BoundField DataField="ProductPrice" HeaderText="Price" />
How can I display a dollor sign before each record in the above column for every row?
You can format your data value as Currency by using DataFormatString
<asp:BoundField DataField="ProductPrice" HeaderText="Price" DataFormatString="{0:C}" />
if the culture is setted to a country who has a dollar currency, it will work fine.
Use TemplateField instead of BoundField, you can format it like anything you want
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
$<%# Eval("ProductPrice")%>
</ItemTemplate>
</asp:TemplateField>
I am doing something like this
dt.Columns.Add("SomeCoumnName");
But I don't want to display this column name after binding it to the gridview.
Just want to show a blank header for particular column.
Set AutoGenerateColumns="false" in Gridview and create custom header like in given sample. If you don't want to show header for a particular column then set HeaderText="" in <asp:BoundField ...>
<asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="County"
DataField="Prop_County" SortExpression="Prop_County" ItemStyle-Width="70px" HeaderStyle-Height="25px">
</asp:BoundField>
<asp:BoundField HeaderText="Sale Date"
DataField="Prop_Sale_Date" ItemStyle-Width="55px"></asp:BoundField>
<asp:BoundField HeaderText="Sale Time"
DataField="Prop_Sale_Time" ItemStyle-Width="55px"></asp:BoundField>
<asp:BoundField HeaderText="Bid Amount"
DataField="Prop_Bid_Amnt" ItemStyle-Width="100px"></asp:BoundField>
</Columns>
</asp:GridView>
I simply added grid view and added columns and gave headertext
But when i run the application i am not able to see any grid,,atleast i should see grid column names
Do i need to do any thing more
Verify that you have everything wired up properly and are assigning a DataSource and doing a DataBind(). Once you have verified that these two things are happening then make sure that your DataSource is returning some type of result set with at least one item.
A GridView will not display anything at all unless there is at least 1 item in the result set. If you bind to a DataSet or some type of object list and there are not items in it then the grid will no display at all. Not even the headers. In this case you should setup the EmptyDataText property to display something.
If not if this helps, please post your GridView markup and the code where you bind your grid and I'll see if I can figure out what the issue is.
check aspx page code
<asp:MyGridView runat="server" DataKeyNames="pkey" AutoUpdateAfterCallBack="true"
Width="100%"
ID="grduser" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Sr No." DataField="rownumber" ReadOnly="true" HeaderStyle-Width="10px"
ItemStyle-Width="10px" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" SortExpression="FirstName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="LoginName" DataField="LoginName" SortExpression="LoginName"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Email" DataField="Email" SortExpression="Email" ReadOnly="true"
HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="Role" DataField="Role" SortExpression="Role" ReadOnly="true"
HeaderStyle-Width="30px" ItemStyle-Width="30px" />
<asp:BoundField HeaderText="Reportingto" DataField="Reportingto" SortExpression="Reportingto"
ReadOnly="true" HeaderStyle-Width="120px" ItemStyle-Width="120px" />
<asp:BoundField HeaderText="MobileNo" DataField="MobileNo" SortExpression="Mobile_no"
ReadOnly="true" HeaderStyle-Width="30px" ItemStyle-Width="30px" />
</Columns>
</asp:MyGridView>
Cs file code to bind grid
DataSet ds = new DataSet();
ds = //get dataset form the database
DataView dv = new DataView(ds.Tables[0]);
this.grduser.DataSource = dv;
this.grdusers.DataBind();
have a look on http://msdn.microsoft.com/en-us/library/ms972948.aspx
Easiest way is as Kelsey says:
<emptydatatemplate>
No Data Found.
</emptydatatemplate>
Other techniques:
1) Override the CreateChildControls (example: http://forums.asp.net/t/1003306.aspx)
2) Manually insert a row (example: http://geekswithblogs.net/dotNETvinz/archive/2009/03/11/tiptrick-show-header-and-footer-of-gridview-when-no-data.aspx)
Is possible set fixed header and scrollbar in a datagrid?
How can i do?
thanks
I worked on this for awhile and gave up on making the CSS work on all browsers. A simple albeit not elegant way to accomplish this is to just have two distinct tables whose column widths match up. The first table holds the headers and the second holds the content and is scrollable.
I used jQuery to make all the column widths match up.
For a complete description, see this post: Scrollable DataGrid table with Fixed Header with jQuery
have at this codeproject entry: Fixed Header in ASP.NET DataGrid
To solve this, I placed a second datagrid above the original, I sized all the columns in both datagrids equally. In the datagrid that shows the data, I set the header to not show. In the code behind I bound the top datagrid to an empty data table and set the bound fields to show the header when empty.
<div style="padding-bottom:2px">
<asp:GridView id="RoleListHeader" runat="server" AutoGenerateColumns="false" showheaderwhenempty="True" showheader="true" height="20px" width="350px" selectedrowstyle-height="20px" rowstyle-height="20px" headerstyle-height="10px" forecolor="#ff333333" backcolor="White">
<columns>
<asp:TemplateField ItemStyle-Width="23">
<ItemTemplate>
<asp:CheckBox></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SystemDesc" HeaderText="Role Description" ReadOnly="true" ItemStyle-Width="174"></asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="EndDate" ReadOnly="true" ItemStyle-Width="153"></asp:BoundField>
</columns>
</asp:GridView>
</div>
<div style="max-height:300px;overflow:auto">
<asp:GridView id="RoleList" runat="server" AutoGenerateColumns="false" showheaderwhenempty="True" showheader="false" height="0px" width="350px" selectedrowstyle-height="20px" rowstyle-height="20px" headerstyle-height="10px" forecolor="#ff333333" backcolor="White" rowstyle-horizontalalign="Center" rowstyle-borderstyle="Solid" rowstyle-bordercolor="#ff404040">
<columns>
<asp:TemplateField ItemStyle-Width="23">
<ItemTemplate>
<asp:CheckBox runat="server" OnCheckedChanged="GVChkBox_CheckChanged" AutoPostBack="true"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SystemDesc" HeaderText="Role Description" ReadOnly="true" ItemStyle-Width="174"></asp:BoundField>
<asp:BoundField DataField="EndDate" HeaderText="EndDate" ReadOnly="true" ItemStyle-Width="153"></asp:BoundField>
<asp:BoundField DataField="ADHSystemId" ShowHeader="false"></asp:BoundField>
</columns>
</asp:GridView>
</div>
take a look at this sample code: (question bellow)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource2"
AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated"
DataKeyNames="Product_Id">
<Columns>
<asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name"
ReadOnly="True" >
<ItemStyle Width="50px" Height="50px" Wrap="true"/>
</asp:ImageField>
<asp:BoundField DataField="Product_Id" HeaderText="Product_Id"
InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
</asp:BoundField>
<asp:BoundField DataField="Product_Name" HeaderText="Product_Name"
SortExpression="Product_Name" />
<asp:BoundField DataField="Category_Name" HeaderText="Category_Name"
SortExpression="Category_Name" ReadOnly="true" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Size" HeaderText="Size"
SortExpression="Size" />
<asp:BoundField DataField="Price" HeaderText="Price"
SortExpression="Price" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Assume I initialize an SqlDataSource, add a parameter and so on.
The thing is, that when a user clicks edit we get a textbox to edit the colnumn value.
I want to validate the data enter by the user before the update is performed and the new
data is propagated back to the server.How?
10x a lot!
You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.
Option 1:
But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.
ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2
Option 2:
But, we can do.
You need to add the validation either the javascript validation or server side validation
control, when the GridView's DataBound event is happening at runtime on the particular
TableCell of the Gridview rows.
Hence, when you click the update button that custom generated javascript or the validation
control will check for the validation on editing the values.
This process is more harder than the converting boundfield to templatefield
refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx
Option 3:
And you can go for server side validation on the values instead of client side validation:
refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx