Gridview columns not being set to width I enter - c#

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>

Related

Why am I not seeing grid lines on my gridview

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.

How can i make my grid view show multiple values (the number of values varies) in a single column cell?

i have to display data like this in grid view:
the problem is that:
for each unique set of manufacturer , model and type, the number of values associated with watts column changes ... that is, there is no fixed numbers of values in this single column. how can i display such a format in my grid view? Kindly help ...
Could you just place another (nested) gridView in that column? You'd have to use a template column in your parent grid, but then for each row, the number of records in that column could be different.
<asp:GridView ID="parentGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Manufacturer" />
<asp:BoundField DataField="ModelNo" HeaderText="Model No" />
<asp:BoundField DataField="Type" />
<asp:TemplateField HeaderText="Watts">
<ItemTemplate>
<asp:GridView ID="nestedWattsView" runat="server" AutoGenerateColumns="true" ShowHeader="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Get selected Index in gridview by clicking on button

I have grid view and there is a template field in it and i want to get the selected row's index by clicking on button i cant change the field type cause i want to change the visibale attribute in my behind code manually.
Here is my code:
<asp:GridView ID="gvService" runat="server" BackColor="White" AutoGenerateColumns="False"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" DataKeyNames="Service_Id, UserId">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" HeaderStyle-BackColor="Gray">
<HeaderStyle BackColor="Gray"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="L_Name" HeaderText="LastName" SortExpression="L_Name"
HeaderStyle-BackColor="Gray">
<HeaderStyle BackColor="Gray"></HeaderStyle>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="bttCost" runat="server" Text="Cost" OnCommand="Cmd_Pay" Visible='<%# IsNotPaid((Decimal)Eval("Cost")) %>' />
</ItemTemplate>
</asp:TemplateField>
and my behind code:
int rowIndex = gvService.SelectedIndex;
HFServiceID.Value = (string)gvService.DataKeys[rowIndex]["Service_Id"];
HFUserID.Value = (string)gvService.DataKeys[rowIndex]["UserId"];
To get the row index the button that was clicked belongs to, add this in your onclick event.
var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;

change Width Gridview While containing 100 words;

HTML
<asp:GridView ID="GridView1" runat="server" GridLines="Vertical" Width="825px" AutoGenerateColumns="False"
DataSourceID="LinqDataSource1">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True"
SortExpression="Title" />
<asp:BoundField DataField="Body" HeaderText="Body" ReadOnly="True" ItemStyleWidth="1"
SortExpression="Body"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DataClassesDataContext"
Select="new (Title, Body)" TableName="PrivateMessages">
</asp:LinqDataSource>
Code
GridView1.Columns[1].ItemStyle.Width = 1;
i would like Fixed Columns[1].width=1 ; While Body containing 100 words;
This Code Does Not Change Width Gridview?
It's not necessary to change the GridView with drag and drop. You have to just select its edges and you can change it.

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