The output is like this:
MyNameIsJohnSmithAnd
Imaperson
What I want is to display it in only one line
MyNameIsJohnSmithAndImaperson
My Aspx gridview code is:
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="5" Font-Names="Calibri"
Font-Size="Medium" Font-Underline="False" ForeColor="Black">
<RowStyle Wrap="False"/>
<EmptyDataRowStyle Wrap="False"/>
<FooterStyle BackColor="Tan" BorderColor="Black" BorderStyle="Solid" Wrap="False"/>
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" Wrap="False" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" Wrap="False"/>
<HeaderStyle BackColor="Tan" BorderStyle="Solid" Font-Bold="True" Wrap="False"/>
<EditRowStyle Wrap="False"/>
<AlternatingRowStyle BackColor="PaleGoldenrod" Wrap="False"/>
</asp:GridView>
I disabled all the wrap property to false in gridview. but the text still wraps.
You have to set the gridview's individual column's word wrap to False as well.
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
<ItemStyle Wrap="False" />
</asp:BoundField>
Try adding this event to your gridview.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
}
}
Here is the reference.
Very simple in .Net Select your grid view (as in Design mode) in property window and follow this
RowStyle -->Font -->>Wrap=False
Thats done
You can do this by css.
#GridView1
{
white-space:nowrap;
}
My column header text was wrapping so I ended up needing a slightly different solution but similar to what user3578419 suggested.
In design view, I edited the 'ColumnHeadersDefaultCellStyle' property and changed the 'WrapMode' value to 'False'.
Related
I have a datatable with sorted column values and when I bind them to the gridview I'm not able to see the same order in the gridview like I have in the datatable.
Sorting event is also disable in gridview.
Can anyone please provide suggestions.
HTML:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found" OnRowDataBound="GridView1_RowDataBound" AllowSorting="true" OnSorting="GridView1_Sorting"
OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" CellPadding="1" PageSize="200" AutoPostBack="true" AllowCustomPaging="true" ShowHeader="true"
CellSpacing="1" BackColor="#e7e7e8" BorderColor="#e7e7e8" GridLines="Both" CssClass="GridViewStyleB" Font-Names="Calibri" Font-Size="10pt">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="4" FirstPageText="First" LastPageText="Last" Position="Top" Visible="true" />
<PagerStyle BackColor="#e7e7e8" ForeColor="Black" HorizontalAlign="left" />
<HeaderStyle CssClass="RowStyle" ForeColor="Black" BackColor="#a9a9a9" Font-Underline="false" HorizontalAlign="Left" />
<RowStyle CssClass="RowStyle" HorizontalAlign="Left" BorderColor="#E7E7E8" ForeColor="Black" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" BorderColor="#E7E7E8" ForeColor="Black" BackColor="#e7e7e8" />
<EmptyDataRowStyle HorizontalAlign="Center" />
</asp:GridView>
Code:
DataTable dtColumns = FromDataTable();
GridView1.DataSource = dtColumns;
GridView1.DataBind();
dtColumns datatable have sorted data and I when I bind them to the gridview the order changes.
If you insert a breakpoint at GridView1.DataSource = dtColumns, and inspect the dtColumns object, can you confirm the sort order of the rows? What is the underlying query used to select the data? If it's from a single table, what data type is the primary key?
I think if you check those things, you will find your answer, but if not, you should update your post to include that stuff :)
EDIT
Here is something you can try to force the sort, after you perform the databinding:
GridView1.Sort("column name", System.Web.UI.WebControls.SortDirection.Descending);
I am trying to prepend some text to what's already in a GridView cell in the RowDataBound event:
protected void gvPatientSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
if (Convert.ToBoolean(dr["Confidential"]))
{
String name = String.Copy(e.Row.Cells[1].Text);
//the problem is the following line
e.Row.Cells[1].Text = "<b><font color='red'>!</font></b>" + name;
}
}
}
For some reason it seems that no matter what I do the original text is deleted. When I remove the line below the comment the name displays normally without the exclamation point; when I add the append code, for some reason the original text becomes a blank string and I only get the exclamation point. I have tried this with and without the String.Copy - I didn't think this would be needed but figured I would try it just in case. No luck.
I don't think this is relevant, but the first column in the row is a hidden column that contains a 1 or a 0. If the value is 1, I want to add the exclamation point. The data is there and this part is being called correctly, I just can't get the name to stay put.
Any ideas why this would be happening?
EDIT: Front end code below
<asp:GridView ID="gvPatientSearch" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True"
PageSize="25" OnPageIndexChanging="gvPatientSearch_PageIndexChanging" OnSorting="gvPatientSearch_Sorting" OnRowDataBound="gvPatientSearch_RowDataBound"
AutoGenerateColumns="false" PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="Right" PagerSettings-Mode="NumericFirstLast" PagerSettings-PageButtonCount="5"
PagerSettings-FirstPageText=" First " PagerSettings-LastPageText=" Last " PagerSettings-PreviousPageText=" Previous " PagerSettings-NextPageText=" Next ">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:BoundField HeaderText="Confidential" DataField="Confidential" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol"></asp:BoundField>
<asp:HyperLinkField DataTextField="PatientName" DataNavigateUrlFields="PatientIdInternal" DataNavigateUrlFormatString="PatientProfile.aspx?InternalID={0}" HeaderText="Patient Name"
SortExpression="PatientName" />
<asp:BoundField DataField="PatientID" HeaderText="Patient ID" SortExpression="PatientID" />
<asp:BoundField DataField="AccountNumber" HeaderText="Account Number" SortExpression="AccountNumber" />
<asp:BoundField DataField="DateOfBirth" HeaderText="Date Of Birth" SortExpression="DateOfBirth" DataFormatString="{0:d}" />
<asp:BoundField DataField="PracticeName" HeaderText="Practice Name" SortExpression="PracticeName" />
<asp:BoundField DataField="PatientIdInternal" HeaderText="Internal ID" SortExpression="PatientIdInternal" Visible="false" />
</Columns>
</asp:GridView>
I can see the values of all the other fields showing up correctly in Visual Studio at this part of the code, only the name field that I am trying to modify shows as an empty string.
A HyperLinkField is rendered as a Hyperlink control, and for this reason you cannot retrieve the cell's text directly. If you want the text, you must retrieve it from the control, not the cell:
HyperLink myLink = e.Row.Cells[1].Controls[0] as HyperLink;
String name = myLink.text;
An alternative would be to grab the name directly from the datasource by doing DataBinder.Eval(e.Row.DataItem, "name") in your code-behind.
I don't know why that's happening, but I think you can work around it by applying you logic with a function like this:
public string Exclaim(bool confidential, string originalText)
{
if (confidential) {
return "<b><font color='red'>!</font></b>" + originalText;
} else {
return originalText;
}
}
In your gridview, invoke the function using:
<asp:TemplateField>
<ItemTemplate>
<%# Exclaim(Eval("Confidential"), Eval("Name"))%>
</ItemTemplate>
</asp:TemplateField>
I think that would work.
I went ahead and did it by changing the offending line to this:
e.Row.Cells[1].Text = "<b>font color='red'>!</font></b> " + Convert.ToString(dr["PatientName"]);
Still don't know why the other way doesn't work, if anyone else does and answers I will mark it correct.
I have a dynamic gridview with data pulled from oracle database.
<asp:GridView ID="gvData" OnRowDataBound="gvData_RowDataBound" OnPreRender="gvData_PreRender" OnSelectedIndexChanged="gvData_SelectedIndexChanged" OnLoad="gvData_Load" OnRowCommand="gvData_RowCommand" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataSourceID="DS" AllowSorting="True" AutoGenerateSelectButton="True" PageSize="10" ShowFooter="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#CCCCCC" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="DS" runat="server">
</asp:SqlDataSource>
I am trying to write DELETE/UPDATE statements. I am having a problem with getting values from gridview and adding them to the parameters os SQL statements.
protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DS.DeleteCommand =
"DELETE FROM TABLE " +
"WHERE PROBLEM_DEPTH_MD = :PROBLEM_DEPTH_MD";
"AND API = :API " +
"AND BUS_AREA_ID = :BUS_AREA_ID";
DS.DeleteParameters.Add(new Parameter("PROBLEM_DEPTH_MD", TypeCode.String, ??GridViewItemValue??));
}
I don't know how to get a value from the gridview. Any suggestions? Thanks!
First off you need to mention the DataKeyName to your gridview of your Primary key Column and then you can access the value of the Selected row of the Gridview like... e.Keys["DataKeyName"]
If you want to access a non key field, then it should be like... e.Values["FieldName"]
Mention DataKeyName like... <asp:GridView ID="gvData" DataKeyNames="PrimaryKeyColumn"
You will need to set the DataKeyNames for you GridView.
You can then use the GridViewDeleteEventArgs to get the Keys property. This will be a dictionary of Key/Value pairs which correspond to the DataKeyFields which you set. In essence it allows you to get your primary keys-- which is what you need to delete.
assuming primary keys are PROBLEM_DEPTH_MD, API, AND BUS_ARE_ID, you would set the DataKeyNames to be "PROBLEM_DEPTH_MD,API,AND BUS_ARE_ID".
You could then grab these keys in your delete event with e.Keys. If you loop over them you should be able to get the values to add your parameters.
Here is some more information on this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
I am using checkboxs in a GridView. I need to determine the value in the cell in the 2nd column, if a checkbox has been checked.
I am using C# 2008 and ASP.net
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" Width="100%" AllowPaging="True" PageSize="20"
onpageindexchanging="gvOrders_PageIndexChanging" ForeColor="#333333">
<Columns>
<asp:TemplateField HeaderText="VerifiedComplete" >
<ItemTemplate>
<asp:CheckBox ID="cbPOID" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PurchaseOrderID" HeaderText="PurchaseOrderID" HtmlEncode="False" ></asp:BoundField>
<asp:BoundField DataField="VENDOR_ID" HeaderText="Vendor ID"></asp:BoundField>
<asp:BoundField DataField="VENDOR_NAME" HeaderText="Vendor Name"></asp:BoundField>
<asp:BoundField DataField="ITEM_DESC" HeaderText="Item Desc"></asp:BoundField>
<asp:BoundField DataField="SYS_DATE" HeaderText="System Date"></asp:BoundField>
</Columns>
<FooterStyle CssClass="GridFooter" BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle CssClass="GridPager" ForeColor="#333333" BackColor="#FFCC66" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle CssClass="GridHeader" BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle CssClass="GridItem" BackColor="#FFFBD6" ForeColor="#333333" />
<AlternatingRowStyle CssClass="GridAltItem" BackColor="White" />
</asp:GridView>
protected void btnDisable_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvOrders.Rows)
{
if (((CheckBox)gvr.FindControl("cbPOID")).Checked == true)
{
string strPrimaryid = gvr.Cells[2].ToString();
}
}
}
Another way to do this, to avoid accessing the cells of the GridView by index, is to convert the desired BoundField to a TemplateField in the designer. Then use the FindControl method to get the text value in that cell. By default, the designer will give the Label control of the new TemplateField a name like "Label1."
I would add some pictures to show the designer part, if that process were easier. At any rate, the aspx would change like so, which you could do manually too:
<asp:BoundField DataField="PurchaseOrderID" ...</asp:BoundField>
becomes
<asp:TemplateField HeaderText="PurchaseOrderID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Then you would make this simple code change:
string strPrimaryid = gvr.FindControl("Label1").Text;
However, I think a better way to do this is by utilizing the DataKeys features of the GridView control. The first step would be to set the DataKeyNames property of your GridView:
<asp:GridView ID="gvOrders" runat="server" DataKeyNames="PurchaseOrderID" ...>
Then, assuming your PurchaseOrderID column is of type int, you would change that same line to be:
int primaryid = (int)gvOrders.DataKeys[gvr.RowIndex].Value;
i have to fix the header in a gridview and i use this in style Class
<style type="text/css">
.gridFixedHeader
{
background-color:white;
position:relative;
top:expression(GridView1.offsetParent.scrollTop-2);
}
</style>
but i get an error that expression(GridView1.offsetParent.scrollTop-2);is not valid...
what all do i need to do to make this work.....
any suggestions
Main aim... to fix the headers so they dont move when the gridview is scrolled up or down...
any help will be appreciated...
here is my gridview code
<asp:Panel ID="Panel1" runat="server" Height="100px" ScrollBars="Vertical">
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="MachineGroupID" DataSourceID="SqlDataSource1">
<RowStyle ForeColor="#000066" />
<HeaderStyle CssClass="gridFixedHeader" />
<Columns>
<asp:BoundField DataField="MachineGroupID" HeaderText="MachineGroupID"
InsertVisible="False" ReadOnly="True" SortExpression="MachineGroupID" />
<asp:BoundField DataField="MachineGroupName" HeaderText="MachineGroupName"
SortExpression="MachineGroupName" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</asp:Panel>
so here i have a gridview where when i scroll the header row moves too.. i dont want that to happen..
It is not valid in W3C. It is an IE only thing. To do what you are looking for in a valid way would require you to run some JavaScript and fix your headers.
If you explain your desired outcome I am sure someone can help. I have made GridView headers look exactly like I want many times.
UPDATE: Here is a good article: http://www.dotnetcurry.com/ShowArticle.aspx?ID=255
When the gridview is rendered to html, the GridView1 ID is probably being rewritten.
you probably want something like:
top:expression(<%= GridView1.ClientID %>.offsetParent.scrollTop-2);
although this may get you what you want it's probably not the best form.
You are heavily advised against using CSS expressions, as they get executed on every event of the browser - including mouseMove. They also don't work on anything but IE.
I can't answer your question further due to lack of details of what you're trying to achieve.
If you're trying to fix it to the top of the viewport try:
.gridFixedHeader
{
background-color:white;
position:fixed;
top:20px; //optional
}
in the elements CSS
edit: just realised you look like you're styling a GridViews header so this will probably not work, try posting some of the Gridview generated HTML and what yo want done with it please