ASP.NET Basic GridView control not displaying at all - asp.net

I am getting started with a basic asp.net gridview control and I just dont seem to be able to make it visible in the web browser at all in an asp.net web application.
Here is what I have just to get myself started off. In the aspx page ...
<asp:GridView ID="myGr" runat="server" BackColor="Aqua" AutoGenerateColumns="True" Width="100%" ViewStateMode="Enabled"></asp:GridView>
In the code behind I have ...
Dim tbl As New DataTable
tbl.Columns.Add("ID", GetType(Int32))
tbl.Columns.Add("Name", GetType(String))
tbl.Columns.Add("Birthday", GetType(Date))
Dim pers As DataRow = tbl.NewRow
pers("ID") = 1
pers("Name") = "Tim"
pers("Birthday") = New Date(1973, 6, 9)
myGr.DataSource = tbl
myGr.DataBind()
Would be great if anyone could give me some basic advice on this control

You need to add the row you inserted to the datatable tbl. Because you haven't done this, the datatable is empty so the Gridview is empty too.
Like that: tbl.Rows.add(pers)

You create row but you do not add it to the table.
You have to add line like that:
tbl.Rows.Add(pers)

Related

ASP.Net DevExpress Gridview exporter CSV download with hyperlink link to website

I'm using DevExpress Gridview exporter to export Gridview to CSV and I wanted the sno field name's row data to become hyperlink and when I click on that it will show webpage of
https://xxx?sno_id
The CSV is exported with hyperlink on sno field name but I have no idea on how to make it works as what I wanted.
I have referred to this site, but still the same outcome.
ASP.net
<dx:ASPxGridViewExporter GridViewID="FeedbackGrid" ID="exportFeedbackGrid" onrenderbrick="exportFeedbackGrid_RenderBrick" runat="server">
</dx:ASPxGridViewExporter>
<dx:ASPxGridView ID="FeedbackGrid" ClientIDMode="Static" OnCustomUnboundColumnData="FeedbackGrid_CustomUnboundColumnData" ClientInstanceName="FeedbackGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<dx:GridViewDataHyperLinkColumn FieldName="sno" Visible="false" PropertiesHyperLinkEdit-NavigateUrlFormatString="Http://{0}">
</dx:GridViewDataHyperLinkColumn>
</Columns></dx:ASPxGridView>
Vb.net
Protected Sub FeedbackGridBut_Click(sender As Object, e As EventArgs)
FeedbackGrid.Columns("sno").Visible = True
exportFeedbackGrid.WriteXlsToResponse()
End Sub
Protected Sub ExportFeedbackGrid_RenderBrick(sender As Object, e As ASPxGridViewExportRenderingEventArgs) Handles exportFeedbackGrid.RenderBrick
Dim dataColumn As GridViewDataColumn = e.Column As GridViewDataColumn
If Not e.RowType = GridViewRowType.Data And dataColumn Is Nothing Then
e.TextValue = e.Value.ToString()
End If
End Sub
anyone has idea on this?
I think you want to display something else on column value but it will create hyperlink with sno column. I suggest you to check the below reference:
Hyperlink Reference to another column DevExpress Gridview ASP.Net Webform
e.g.
GridViewDataHyperLinkColumn colItemName = new GridViewDataHyperLinkColumn();
colItemName.FieldName = "sno";
colItemName.PropertiesHyperLinkEdit.NavigateUrlFormatString = "~/details.aspx?Device={0}";
colItemName.PropertiesHyperLinkEdit.TextFormatString = "{0}";
colItemName.PropertiesHyperLinkEdit.TextField = "AnotherColumnFieldName";
ASPxGridView1.Columns.Add(colItemName);
References:
ASPxGridView - How to customize HyperLink column
How to create and configure a HyperLink column at runtime
If does not help then provide more details about the required functionality.

ASP.NET GridView Paging Not Working

I have searched through stack overflow and Google but I could not find an answer that would work for my situation and I tried a lot of them.
I have an issue with an ASP.NET gridview. When the page first loads the grid is attached to a Data Source ID and displays the records with paging that works.
Now if the user filters the data the first page of returned records is displayed but if they click on 'Page 2' in the gridview footer the page flashes but does not change the data or pages.
Also I have noticed that when the search is applied there are 10 plus pages displayed in the footer no matter how many records are returned.
Here is the ASP.NET GridView header code:
<asp:GridView ID="gvSOHeader" runat="server"
AutoGenerateColumns="False" Width="1010px" CellPadding="4"
ForeColor="#333333" GridLines="Horizontal" AllowSorting="True"
AllowPaging="True" OnPageIndexChanging="gvSOHeaderOnPageIndexChanging"
PageSize="15" DataSourceID="SqlDataSource2">
When the user searches here is the code that reassigns the grid view datasource:
Using conn As New SqlConnection(myConnection)
conn.Open()
Using sqlCmd As New SqlCommand
sqlCmd.CommandText = strText
sqlCmd.Connection = conn
Using sqlDA As New SqlDataAdapter(sqlCmd)
Using sqlDT As New DataTable
sqlDA.Fill(sqlDT)
If sqlDT.Rows.Count > 0 Then
gvSOHeader.DataSourceID = String.Empty
gvSOHeader.DataSource = Nothing
gvSOHeader.DataSource = sqlDT
gvSOHeader.DataBind()
End If
End Using
End Using
End Using
conn.Close()
End Using
This works but only is as far as it displays the first page of returned results. I can not page to any other results page.
Here is the OnPageIndexChanging event code:
Protected Sub gvSOHeaderOnPageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gvSOHeader.PageIndexChanging
gvSOHeader.PageIndex = e.NewPageIndex
gvSOHeader.DataBind()
End Sub
I have also noticed that the OnPgeIndexChanging event fires twice but I do not know why. I tried adding an If statement to the OnPageIndexChanging to check for Not IsPostback but it still will not display more than the first page of searched data.
I am sorry if this is a duplicate but I could not find a result that fit my situation. Any help would be very greatly received.
Thanks.

ASP.NET : GridView in dynamics accordionPanes Ajax

What i'm trying to do : I have some meeting date in my database. For display this, I want make one GridView for each day. (I have several meetings by days). And after, put this gridview in accordionPane (one by day). I want put in Pane for not display a long list in the screen.
I know how make dynamics accordionPanes, I know make dynamics GridView (depends on the number of meetings).
I know put one GridView in one accordionPane. (juste for statics one)
But I can't put dynamically GridViews in accordionPanes...
The source code of my problem :
The way to create dynamic number of accordionPanes :
Code Behind :
For i = 1 To j
Dim volet As New AjaxControlToolkit.AccordionPane
volet.Visible = True
volet.ID = "volet" & i.ToString
Accordion1.Panes.Add(volet)
Next
Code ASP.NET :
<asp:Accordion ID="Accordion1" runat="server" Width="873px" CssClass="accordion" FadeTransitions="true" HeaderCssClass="accordionHeader" HeaderSelectedCssClass="accordionHeaderSelected" RequireOpenedPane="false" ContentCssClass="accordionContent" TransitionDuration="100">
</asp:Accordion>
The way to create dynamic number of GridView :
Code behind :
For i = 1 To j
Dim datag As New GridView
datag.Visible = True
datag.ID = "datag" & i.ToString
Panel1.Controls.Add(datag)
Next
Now I try to insert GridView in accordionPanes. That's my issue.
I hope i'm clear, ask me if i'm not well expressed.
Thanks a lot for your answers
I found my answer !
I missed two functions : pane.ContentContainer.Constrols.Add and pane.HeaderContent.Controls.Add.
For i = 1 To j
Dim datag As New GridView
Dim title As New Label
datag = GenererGridView(i) 'GridView from database
datag.Visible = True
datag.ID = "datag" & i.ToString
title.Text = "datag" & i.ToString
Dim pane As New AjaxControlToolkit.AccordionPane
pane.Visible = True
pane.ID = "pane" & i.ToString
pane.ContentContainer.Controls.Add(datag)
pane.HeaderContainer.Controls.Add(title)
Accordion1.Panes.Add(pane)
Next

ASP Stored Procedure to GridView

I am attempting to use an existing stored procedure to populate a gridview.
First, I execute the stored procedure and use a SqlAdapter to put it into a DataSet. I know this works because DataSet.Tables[0] contains my data. However, when I create a GridView and bind the data to the GridView, nothing is displayed.
Here is the code for binding the GridView:
DataSet ds = Execute_spr();
GridView testGridView = new GridView();
if (ds.Tables.Count > 0)
{
testGridView.DataSource = ds.Tables[0].AsEnumerable();
testGridView.DataBind();
}
and here is the code for my gridview in the .aspx page:
<asp:GridView ID="testGridView" runat = "server" AutoGenerateColumns = "true" />
Any idea what I might be doing incorrectly?
Edit: I have tried the ds.Tables[0] without AsEnumerable() and using .DefaultView
Why are you re-initialising the Gridview in the line
GridView testGridView = new GridView();
Create a protected member in your codebeind called "testGridView", remove the line above, and you might start to get somewhere...

"Both DataSource and DataSourceID are defined" error using ASP.NET GridView

"Both DataSource and DataSourceID are defined on 'grdCommunication'. Remove one definition."
I just got this error today, the code has been working until this afternoon I published the latest version to our server and it broke with that error both locally and on the server. I don't use "DataSourceID", the application reads database queries into a datatable and sets the datatable as the DataSource on the GridViews. I did a search in Visual Studio, searching the entire solution and the string "DataSourceID" does not appear in even 1 line of code in the entire solution. This is the first thing that freaked me out.
I figure it had been working yesterday, so I reverted the code to yesterday's build. The error was still there. I kept going back a build, and still the issue is there. I went back a month, I am still getting the same error. This application was working fine this morning? There has really been no code changes, and no where in the application is the DataSourceID EVER set on any of the gridviews. Has anyone ever seen anything like this at all??
How can I get that error if DataSourceID is never set... and the word "DataSourceID" is not in my solution? I just did a wingrep on the entire tree doing a case insensitive search on datasourceid.... pulled up absolutely nothing. That word is absolutely no where in the entire application.
<asp:GridView ID="grdCommunication" runat="server"
Height="130px" Width="100%"
AllowPaging="true" >
... standard grid view column setup here...
</asp:GridView>
// Code behind.. to set the datasource
DataSet dsActivity = objCompany.GetActivityDetails();
grdCommunication.DataSource = dsActivity;
grdCommunication.DataBind();
// Updated: removed some confusing notes.
Try this:
DataSet dsActivity = objCompany.GetActivityDetails();
grdCommunication.DataSource = dsActivity.Tables[0];
grdCommunication.DataBind();
Holy smoke batman. The Table name was changed causing my Datasource to be no good. But that error message doesn't make any sense in this situation. So technically tsilb's solution will work if I call the table by index instead of by name, so I'll mark his solution as correct.
After reading his post, I tried dsActivity.Tables["Activities"] instead of passing the dataset to the Datasource and the table name to the Datamember, and obviously that didn't work, but If I pass the actual index, which I don't like doing because that index might change, then it is now working. But the messed up part, was that error.. That error was completely off base as to what the problem was. saying that I defined both and to remove one, when in reality, that was not the case. and another really messed up thing, was the table name was only changed to be all upper case... But hey, "Activities" is a different key than "ACTIVITIES".
Replace this code before this grdCommunication.DataSource = dsActivity;
grdCommunication.DataBind();
grdCommunication.DataSourceID="";
tslib is right, don't do:
grdCommunication.DataSourceID = null;
or the string.Empty version. You only use the DataSourceID if you're using a SqlDataSource or ObjectDataSource control for your binding.
It's called "declarative" binding because you're using "declared" controls from on your page. Binding to controls does not require a call to the DataBind() method.
Because you're DataBinding manually (calling grd.DataBind()) you only set the DataSourrce and then call DataBind().
I ran into the same error, but a totally different problem and solution. In my case, I'm using LINQ to SQL to populate some dropdown lists, then caching the results for further page views. Everything would load fine with a clear cache, and then would error out on subsequent page views.
if (Cache["countries"] != null)
{
lbCountries.Items.Clear();
lbCountries.DataValueField = "Code";
lbCountries.DataTextField = "Name";
lbCountries.DataSource = (Cache["countries"]);
lbCountries.DataBind();}
else
{
var lstCountries = from Countries in db_read.Countries orderby Countries.Name select Countries;
lbCountries.Items.Clear();
lbCountries.DataValueField = "Code";
lbCountries.DataTextField = "Name";
lbCountries.DataSource = lstCountries.ToList();
lbCountries.DataBind();
Cache.Add("countries", lstCountries, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
}
The issue came from:
Cache.Add("countries", lstCountries, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
When it should have been:
Cache.Add("countries", lstCountries.ToList(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 240, 0), System.Web.Caching.CacheItemPriority.High, null);
I got this error today, turns out that it had nothing to do with DataSourceID, and had everything to do with the DatasSource itself.
I had a problem in my DatasSource , and instead of getting a DatasSource related error, I got this meaningless error.
Make sure you're DatasSource is good, and this error should go away.
always bind dataset with table index to gridview...
ex. gridgrdCommunication.Table[0]; as metioned above by Tsilb
second way you intentionally write..
gridgrdCommunication.DataSourceID = String.Empty;
gridgrdCommunication.DataSource=ds;
gridgrdCommunication.DataBind();
Check you database structure.... if you are acceding your data throw a dbml file, the table structure in your database it's different of the dbml file structure
If you are using the Object Data Source and want to conditionally reload the grid in code behind you can successfully do this:
Dim datatable As DataTable = dataset.Tables(0)
Dim dataSourceID As String = gvImageFiles.DataSourceID
gvImageFiles.DataSourceID = Nothing
gvImageFiles.DataSource = datatable.DefaultView
gvImageFiles.DataBind()
gvImageFiles.DataSource = Nothing
gvImageFiles.DataSourceID = dataSourceID
You need to chose one way to bind the grid
if it is from code behind means using c# code then remove the datasourceid property from grid view from design view of grid
like this
//you have to make it like this
Please try this:
gvCustomerInvoiceList.DataSourceID = "";
gvCustomerInvoiceList.DataSource = ci_data;
gvCustomerInvoiceList.DataBind();
I got this error today. It turns out that my stored procedure did not return neither any record nor a structure. This was because I had an empty try catch without a raiserror.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.DataBind()
End Sub
Function GetData()
Dim dt As New DataTable
Try
dt.Columns.Add("ROOM_ID", GetType(String))
dt.Columns.Add("SCHED_ID", GetType(String))
dt.Columns.Add("TIME_START", GetType(Date))
dt.Columns.Add("TIME_END", GetType(Date))
Dim dr As DataRow = dt.NewRow
dr("ROOM_ID") = "Indocin"
dr("SCHED_ID") = "David"
dr("TIME_START") = "2018-01-03 09:00:00.000"
dr("TIME_END") = "2018-01-03 12:00:00.000"
dt.Rows.Add(dr)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Return dt
End Function
and add this to your item DataSource="<%# GetData() %>"
In my case the connection string to the database was not working. Fixing the connection string got rid of this error.

Resources