ASP.NET 3.5 Gridview control - asp.net

If you click on the 'search' button in the following link
link text
A gridview shows up. I am trying to do something similar within a table whose width is set to auto. My current way is to create the gridview in design view, but however, my gridview does not resize to the table width. I understand that I posed 2 different questions in one.
My current gridview is as such. Defined in 'source' view
<asp:GridView ID="gridView" runat="server"
AutoGenerateColumns="False"
EnableSortingAndPagingCallbacks="True"
AllowPaging="True" DataSourceID="FilesByJobObjectDataSource"
PageSize="5" OnRowCommand="gridView_RowCommand" DataKeyNames="FileID"
HorizontalAlign="Left" >
<Columns>
<asp:BoundField DataField="RID" HeaderText="RID"
ReadOnly="True" ItemStyle-Width="50px" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Category" HeaderText="Category"
ReadOnly="True" ItemStyle-Width="100 px" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="FileName" HeaderText="Type"
ReadOnly="True" ItemStyle-Width="575 px" >
<ItemStyle Width="575px" />
</asp:BoundField>
<asp:BoundField DataField="FileID" Visible="false" />
<asp:ButtonField Text="X" ButtonType="Button" ItemStyle-Width="20px" >
<ItemStyle Width="20px" />
</asp:ButtonField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
</ContentTemplate>

Don't set the column widths....

Related

ASP.NET GridView Needs two Different Action Buttons

I've got a GridView that I'd like to have two buttons with two different actions. I had tried making both of them select buttons, which would be optimal, but I can't get ASP.NET to tell me which of the two buttons triggered the event. It'll tell you the row index, but not the column from what I see.
I changed one of my buttons to an edit button so that it then calls a different method, but then it puts the row into edit mode. I do not see a way to cancel the edit, AND it is mis-using the intended use of the code.
The buttons are in the first and last columns of the gv.
<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ButtonType="Button" SelectText=" -STOP- " ShowSelectButton="True" ShowCancelButton="False" />
<asp:CheckBoxField DataField="Active_Med" HeaderText="Active" SortExpression="Active_Med" >
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:BoundField DataField="Medication_List_ID" HeaderText="Medication_List_ID" InsertVisible="False" ReadOnly="True" SortExpression="Medication_List_ID" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Label_Name" HeaderText="Medication" SortExpression="Label_Name" >
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Form" HeaderText="Form" SortExpression="Med_Form" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="dose" HeaderText="Dose" SortExpression="dose" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="dose_unit" HeaderText="Unit" SortExpression="dose_unit" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Amt" HeaderText="Med_Amt" SortExpression="Med_Amt" Visible="False" />
<asp:BoundField DataField="Amount_Unit" HeaderText="Amount_Unit" SortExpression="Amount_Unit" Visible="False" />
<asp:BoundField DataField="Med_Sched_Label" HeaderText="Frequency" SortExpression="Med_Sched_Label" >
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Dispense" HeaderText="Dispense" SortExpression="Med_Dispense" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Dispense_Unit" HeaderText="Unit" SortExpression="Dispense_Unit" ShowHeader="False" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Refill" HeaderText="Med_Refill" SortExpression="Med_Refill" Visible="False" />
<asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
<asp:CommandField ButtonType="Button" EditText="-REFILL-" ShowCancelButton="False" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#27627E" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" Width="125px" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
Use the CommandName property
Front-End
<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton" OnRowCommand="gvMedList_RowCommand">
<Columns>
<asp:TemplateField HeaderText="ColumnName">
<ItemTemplate>
<asp:Button ID="btnDoSomething" runat="server" CommandName="DoSomething" Text="Do Something" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AnotherColumn">
<ItemTemplate>
<asp:ImageButton ID="btnImageSomething" runat="server" CommandName="DoSomethingElse" ImageUrl="~/images/yes.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-Behind (assumed C#...if you need VB let me know)
protected void gvMedList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DoSomething")
{
// code to execute
}
if (e.CommandName == "DoSomethingElse")
{
// code to execute
}
}

EmptyDataTemplate Does not show when Empty

I have a gridview that has a EmptyDataTemplate, but it does not show when there is no data.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="TransID"
Font-Size="Small" GridLines="Vertical" HorizontalAlign="Center" PageSize="50"
AutoGenerateDeleteButton="True" AllowSorting="True" >
<PagerSettings FirstPageText="First" LastPageText="Last" Mode="NextPreviousFirstLast"
NextPageText="Next" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="TransID" HeaderText="Transaction ID" InsertVisible="False"
ReadOnly="True" SortExpression="TransID">
<HeaderStyle Font-Underline="False" />
</asp:BoundField>
<asp:BoundField DataField="LicNumFrom" HeaderText="License Number From" SortExpression="LicNumFrom" />
<asp:BoundField DataField="LicNameFrom" HeaderText="Name From" SortExpression="LicNameFrom" />
<asp:BoundField DataField="LicNumTo" HeaderText="License Number To" SortExpression="LicNumTo" />
<asp:BoundField DataField="LicNameTo" HeaderText="Name To" SortExpression="LicNameTo" />
<asp:BoundField DataField="DateOfDelivery" HeaderText="Date Of Delivery" SortExpression="DateOfDelivery"
DataFormatString="{0:MM/dd/yyyy}" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#2D5278" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
<EmptyDataTemplate>
<div>
No Data Available
</div>
</EmptyDataTemplate>
</asp:GridView>
All it shows is a little square. I've tried putting a Label in the EmptyDataTemplate, but it doesn't work either.
You should check the CSS style of EmptyDataTemplate. It looks like you are getting it in a square box but texts are same as your background color and so they are lost in the background. You are getting that square box because you are applying 1px black border to your GridView which also gets applied to EmptyDataTemplate by default.
Try to apply EmptyDataRowStyle-ForeColor="#000" attribute to your GridView with any color which is not same as GridView background color and check the result.

filter gridview using checkbox

I have a gridView and two checkboxes that will be used for filtering..
the first checkbox(Accepted), when checked, the gridview will only show data from the database that have the status Accepted.
Here's my gridview and checkbox:
<div style="height: 250px; overflow-x: hidden; overflow-y: scroll;" >
<asp:CheckBox ID="Accepted" runat="server" />
<asp:CheckBox ID="Pending" runat="server" />
<asp:CheckBox ID="Rejected" runat="server" />
<asp:GridView ID="gvtransaction" runat="server" Width="30%" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="5px" DataKeyNames="id" GridLines="Horizontal" OnRowDataBound="gvtransaction_RowDataBound" OnRowCommand="TransactionStatus">
<Columns>
<asp:BoundField DataField="MerchantID" HeaderText="ID" SortExpression="" />
<asp:BoundField DataField="FirstName" HeaderText="Consumer" SortExpression="" />
<asp:BoundField DataField="LastName" HeaderText="Name" SortExpression="" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="" />
<asp:BoundField DataField="CurrencyName" HeaderText="Account Name" SortExpression="" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="" />
<asp:ButtonField ButtonType="Button" CommandName="Accept" HeaderText="Action" ShowHeader="True" Text="Accept" />
<asp:ButtonField ButtonType="Button" CommandName="Reject" HeaderText="Action" ShowHeader="True" Text="Reject" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
How can i do this using javascript.?
thank you...
You can use following properties of gridview 'onrowdatabound and DataKeyNames'.
In DataKeyNames you can give the name of your class variables or properties where you are reading from database through a datareader.
Fire the rowDatabound event of your gridview so that you can check the status
protected void gvtransaction_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
int Status = (int)this.gvtransaction.DataKeys[e.Row.RowIndex].Values[0];
if (Status)
{
.
check status values for Accepted, pending and rejected through if else.
.
.
}
}
You can create 2 Grid views and when one of the check boxes selected bind the associated grid view.

How to Insert Date and Time inside the Gridview

Again, New issue. Please be guided to the image below.
Here's the output (see the image below):
Here's the code of my Gridview in results.aspx
<asp:GridView runat="server" id="GridView1" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" DataKeyNames="ID" BorderStyle="Ridge" BackColor="White" BorderColor="Black" BorderWidth="3px" CellPadding="3" Width="1000px" AllowPaging="True" PageSize="2">
<RowStyle BackColor="White" ForeColor="#003399" HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:boundfield DataField="Time In" HeaderText="Time In" SortExpression="Time In">
<ItemStyle Width="100px" />
</asp:boundfield>
<asp:boundfield DataField="Username" HeaderText="Bar Code No." SortExpression="Username">
</asp:boundfield>
<asp:boundfield DataField="FirstName" HeaderText="First Name" SortExpression="FirstName">
</asp:boundfield>
<asp:boundfield DataField="LastName" HeaderText="Last Name" SortExpression="LastName">
</asp:boundfield>
<asp:boundfield DataField="MiddleName" HeaderText="Middle Name" SortExpression="MiddleName">
</asp:boundfield>
<asp:boundfield DataField="ContactNumber" HeaderText="Contact No." SortExpression="ContactNumber">
</asp:boundfield>
<asp:boundfield DataField="PlateNumber" HeaderText="Plate No." SortExpression="PlateNumber">
</asp:boundfield>
<asp:boundfield DataField="Color" HeaderText="Color" SortExpression="Color">
</asp:boundfield>
<asp:boundfield DataField="Brand" HeaderText="Brand" SortExpression="Brand">
</asp:boundfield>
<asp:boundfield DataField="LiscensedNumber" HeaderText="Liscensed No." SortExpression="LiscensedNumber">
</asp:boundfield>
<asp:templatefield>
<HeaderTemplate>
Image
</HeaderTemplate>
<ItemTemplate>
<img src="data:image/jpg;base64,<%# Eval("Image") != System.DBNull.Value ? Convert.ToBase64String((byte[])Eval("Image")) : string.Empty %>" alt="image" height="85" width="85" />
</ItemTemplate>
</asp:templatefield>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle HorizontalAlign="Center" BackColor="#999999" ForeColor="#003399" />
<EmptyDataTemplate>
No data found!
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A0A0A0" Font-Bold="True" ForeColor="#003399" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
Please help. I don't know the code regarding "Date and Time" thing.
Try removing space in your datafield, as by Rahul R.
It does not matter in the Header Text but in datafield it does.
<asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
also make sure there are no spaces in datafield name
Sorry for asking here, I solved it.
I convert the column in templatefield then I added this code
Text='<%# DateTime.Now.ToString("dddd<br /> MMMM dd, yyyy<br /> hh:mm tt") %>
:) thanks to all of you.

GridView automatically re-sizing itself

I have a GridView in asp that keeps re-sizing itself. Can i in some way fix the size of the GridView so it doesn't do that ? The GridView keeps shrinking itself although the data in it has the same length or is smaller
my asp code:
<asp:GridView ID="Grid" runat="server" AllowPaging="True" PageSize="18" OnPageIndexChanging="Grid_PageIndexChanging"
ForeColor="Black" GridLines="Both" Width="991px" Height="600px" RowStyle-HorizontalAlign="Center"
BackColor="White" BorderColor="#999999" BorderWidth="1px"
AutoGenerateColumns="False" style="margin-left: 9px">
<Columns>
<asp:TemplateField HeaderText="RequestID">
<ItemTemplate>
<a target = "_blank" href="www.danx/default?request=<%# Eval("request")%>"><%# Eval("request")%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Barcode">
<ItemTemplate>
<a target = "_blank" href="www.danx/default?barcode=<%# Eval("barcode")%>"><%# Eval("barcode")%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="adrid" HeaderText="AdrID" SortExpression="AdrID" />
<asp:BoundField DataField="name" HeaderText="Navn" SortExpression="Name" />
<asp:BoundField DataField="street" HeaderText="Vej" SortExpression="Street" />
<asp:BoundField DataField="houseno" HeaderText="Husnr"
SortExpression="HouseNo" />
<asp:BoundField DataField="postal" HeaderText="Postnr"
SortExpression="Postal" />
<asp:BoundField DataField="city" HeaderText="By" SortExpression="City" />
<asp:BoundField DataField="country" HeaderText="Land"
SortExpression="Country" />
<asp:TemplateField HeaderText="Lokation">
<ItemTemplate>
<a target = "_blank" href="https://maps.google.dk/maps?q=<%# Eval("latitude")%>,<%# Eval("longitude")%>"><%# Eval("latitudetxt")%></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="date" HeaderText="ReceivedDate"
SortExpression="ReceivedDate" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<RowStyle HorizontalAlign="Center"></RowStyle>
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Blue" HorizontalAlign="Center" />
<HeaderStyle BackColor="DarkGray" Font-Bold="True" ForeColor="Black" />
<AlternatingRowStyle BackColor="LightGray" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
Can you help me please?
Since you are using a fixed width (991px) for the gridview, the columns would shrink/adjust as their content grow larger. One thing you could do is give fixed width to those columns you don't want resized (in % or fixed). e.g.
<ItemTemplate>
<a target = "_blank" href="www.danx/default?request=<%# Eval("request")%>"><%# Eval("request")%></a>
</ItemTemplate>
You can also increase the size of the GridView to easily accomodate the columns e.g. Setting it to 100% if the container is greater 991px
is this maybe to do with the data in the cells being too long without spaces.. you can always use
td{word-wrap:break-word;}
or make sure you have spaces.
or even use styles for the columns, which set the widths explicitly
<asp:TemplateField HeaderText="RequestID" HeaderStyle-CssClass="setWidthClass">
<ItemTemplate>...
or even
<asp:TemplateField HeaderText="RequestID" ItemStyle-Width="100">
of course it may be nothing to do with this.

Resources