Why am I not seeing grid lines on my gridview - c#

I have a gridview on my web page.
I am able to populate the Grid using C# in the behind code.
Here is a sample screen of 2 columns and 2 rows:
What I don't understand is why there Gridlines are not showing on the rows of data when I have it set on the gridview properties to show "both".
<asp:GridView id="gvappts"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
BorderColor="#EFE6F7"
ForeColor="#003399"
Width="100%"
Font-Size="Small"
GridLines="Both"
BorderStyle="Solid"
BorderWidth="1px"
CellPadding="3">
<Columns>
<asp:BoundField DataField="apptid" HeaderText="Appt #" />
<asp:BoundField DataField="name" HeaderText="Name" />
</Columns>
</asp:GridView>
What am I missing?

Please try the below code to your row border..
<RowStyle HorizontalAlign="Center" BackColor="White" BorderColor="#0000CC"
BorderStyle="Solid" BorderWidth="2px"/>
And also try the same to columns.

All of the comments on this post are correct to some extent which is, you mention that the issue must rely in the styles, after going line by line in my style sheets I found the culprit style and was able to get the grid-lines to show.

Related

Gridview columns not being set to width I enter

I have two gridviews one displaying under the other one. Both gridviews are exactly the same, the only difference with the second gridview is I am not showing the header column. I have tried setting the widths to the exact same values in both gridviews but the columns are not matching up.
I have tried setting with width on the boundfield:
<asp:BoundField DataField="Code" HeaderText="A/C" SortExpression="Code" ItemStyle-Width ="120px"></asp:BoundField>
I have tried setting the width of the column:
gridview.Columns[0].ItemStyle.Width = 120;
I have tried setting the width of the cells:
foreach (TableRow row in gridview.Controls[0].Controls)
{
row.Cells[0].Width = 120;
}
None of the above have worked. The second gridview width (last row in the image) is not displaying the same as the first gridview
First Grid:
<asp:GridView runat="server"
id="gvJobsPerMonth"
CssClass="tblResults"
OnItemDataBound="gvJobsPerMonth_OnItemDataBound"
AllowSorting="true"
OnSortCommand="gvJobsPerMonth_Sort"
DataKeyField="ID"
AutoGenerateColumns="false" >
<HeaderStyle CssClass="tblResultsHeader" />
<AlternatingRowStyle BackColor="#EEEEEE" />
Second Grid:
<asp:GridView runat="server"
id="gvJobsPerMonthTotals"
CssClass="tblResults"
OnItemDataBound="gvJobsPerMonthTotals_OnItemDataBound"
AllowSorting="true"
OnSortCommand="gvJobsPerMonthTotals_Sort"
DataKeyField="ID"
AutoGenerateColumns="false"
ShowHeader="false" >
<HeaderStyle CssClass="tblResultsHeader" />
<AlternatingRowStyle BackColor="#EEEEEE" />
Both grids are using the same CSS classes. The only difference is ShowHeader="false" on the second grid. But even if I set this to true the columns are still showing as different widths
Try this code for your second GridView:
<RowStyle Width="120px"/>
Instead of 120px, use the desired pixel, just after column close </Columns>, like this:
</Columns>
<RowStyle Width="120px"/>
</asp:GridView>
The width of a GridView is set with the ItemStyle Width property.
<asp:BoundField DataField="language" HeaderText="language">
<ItemStyle Width="500px" />
</asp:BoundField>

Sorted Datatable value differs in UI gridview in c#

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);

how to insert rows into gridview

Sir,
I want to have an insert link functionality to insert rows into gridview As we have auto generate edit and delete in gridview in asp.net,. how to do it?can u also provide me the links.
Hope i have understood your question and this solution might help you..
Solution:
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" AllowSorting="true"
AutoGenerateColumns="False" PageSize="2" AutoGenerateDeleteButton="true" AutoGenerateEditButton="true"
DataKeyNames="theID" DataSourceID="SqlDataSource1" >
<PagerSettings Mode=NumericFirstLast />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="Default2.aspx">HyperLink</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="theID" HeaderText="ID" ReadOnly="true" SortExpression="theID" />
<asp:BoundField DataField="theName" HeaderText="Name" SortExpression="theName" />
</Columns>
Use databinding to insert the row in theposition you want and after bind it to control, if you're talking about WindowsForms.
If youare talking about WPF, do the same :)
If you use ASP.NET insert the row into the binded data and "rebind" it.

css problem in top:?

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

ASP.NET/GridView: Using only a subset of the data fields in the table

I have a list of objects called Activity and I want to display the date, type and notes for each and every one of these activities. This is the code I'm using.
<asp:GridView ID="gvTable" runat="server" AllowSorting="true" ShowHeader="true">
<Columns>
<asp:BoundField DataField="ActivityDate" HeaderText="Date"
HeaderStyle-CssClass="date" />
<asp:BoundField DataField="ActivityType" HeaderText="Type" />
<asp:BoundField DataField="ActivityNotes" HeaderText="Notes" />
</Columns>
<PagerSettings Position="Bottom" Mode="NextPrevious" PageButtonCount="5"
PreviousPageText="Older activities" NextPageText="Newer activities" />
</asp:GridView>
However, all of the attributes of each object is displayed in the header. How can I force it to display only the columns that I want to use?
gvTable.AutoGenerateColumns = false
or
<asp:GridView ID="gvTable" runat="server" AutoGenerateColumns="False" AllowSorting="true" ShowHeader="true">
should do the trick.
Set the attribute on your gridview:
AutoGenerateColumns="false"
You need to set the AutoGenerateColumns property on the grid to false.
Have you tried AutoGenerateColumns="false" in the gridview?

Categories