How to display data from gridview - asp.net

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)

Related

Gridview BoundField Remove Time from DateTime In Edit/Update Mode

I have the following GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="EVENT,TERM_CODE" DataSourceID="OracleDataSource" ShowHeaderWhenEmpty="True"
EnableViewState="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="EVENT" HeaderText="Event" ReadOnly="True"
SortExpression="EVENT" />
<asp:BoundField DataField="TERM_CODE" HeaderText="Term Code" ReadOnly="True"
SortExpression="TERM_CODE" />
<asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date"
SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}" />
</Columns>
</asp:GridView>
The SNAPSHOT_DATA_DATE column is a DateTime field in the database.
I only want to show the date not both the date and time ex. 7/25/2012
So I added DateFormatString="{0:d}" to the BoundField and this works great when viewing the Gridview. However, if I click the edit link to update the date it displays as: 7/25/2012 12:00:00 AM The DateFormatString is ignored in edit mode. Can someone please tell me how to remove the time when in edit / update mode? I don't want users dealing with the time only the date.
Make sure you have set the following properties for formatting to work in Edit Mode in Bound field.
DataFormatString = ({0:d})
ApplyFormatInEditMode = True
In your code if you just modify like below, it should work fine
<asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date" ApplyFormatInEditMode="true" SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}" />
I specifically wanted to format my date as yyyy/MM/dd without the time equivalent to the following:
string dateString = DateTime.Now.ToString("yyyy/MM/dd");
To do this I just added the format on the DataFormatString attribute for the BoundField:
<asp:BoundField DataField="DatePosted" HeaderText="Date" DataFormatString="{0:yyyy/MM/dd}" />

Changing default sort order for listview and gridview to descending

I have a gridview and listview in my aspx pages which both display tables from my database, the first column field displayed is the ID field. I would like the ID in descending order by default when the aspx pages load up, so that the data displayed is from newest to oldest(I'm making a review website). I'm not sure how to do this, when the page loads up, it displays data in ascending order instead. Users are able to click the ID column and change the order, but that's not what I want.
Any help on how to do this would be much appreciated!
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="DataSource">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Date" HeaderText="Date"
SortExpression="Date" />
<asp:BoundField DataField="CPUModel" HeaderText="CPUModel"
SortExpression="CPUModel" />
<asp:BoundField DataField="Manufacturer" HeaderText="Manufacturer"
SortExpression="Manufacturer" />
<asp:ImageField DataImageUrlField="Picture">
</asp:ImageField>
</Columns>
</asp:GridView>
Can't you just order by ID through your SQL datasource queries?

Custom message for BoundField in gridview in HTML markup

I have a BoundField in my gridview. Some of these BoundField are empty if there is no data in the datasource. For such type of fields I want to show N/A, but from the HTML markup only. Is there any way to do so???
This is the code for my grid view which has empty values and for what I want to show N/A
<asp:BoundField DataField="POCreatedDate" HeaderText="Created On" DataFormatString=" {0:MM/dd/yyyy}"
HtmlEncode="false" SortExpression="POCreatedDate"/>
<asp:BoundField DataField="PODueDate" HeaderText="Due Date" DataFormatString="{0:MM/dd/yyyy}"
HtmlEncode="false" SortExpression="PODueDate"/>
NullDisplayText property is all you need :
<asp:BoundField DataField="POCreatedDate" HeaderText="Created On"
DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false"
SortExpression="POCreatedDate"
NullDisplayText="N/A" />

don't want to add column name in datatable

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>

Adding a validator to the gridview textbox, created in edit-mode of a bound field

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

Resources