i have a GridView bound to a DataView.
Some columns in the DataView's table are foreignkeys to related tables(f.e. Customer). I want to enable sorting for these columns too, but all i can do is sorting the foreignkey(fiCustomer) and not the CustomerName.
I have tried this without success(" Cannot find column ERP_Customer.CustomerName "):
<asp:TemplateField HeaderText="Customer" SortExpression="ERP_Customer.CustomerName" >
A tried also the DataViewManager, but i've a problem to detect the table to sort:
Dim daCharge As New ERPModel.dsERPTableAdapters.ERP_ChargeTableAdapter
daCharge.Fill(dsERP.ERP_Charge)
Dim viewManager As New DataViewManager(Me.dsERP)
viewManager.DataViewSettings(dsERP.ERP_Charge).RowFilter = filter
viewManager.DataViewSettings(dsERP.ERP_Charge).Sort = sort 'sort is the GridView's SortExpression
Me.GrdCharge.DataSource = viewManager.CreateDataView(dsERP.ERP_Charge)
I have to apply the sort on a distinct table of the DataViewManager, but this table would differ on the related tables.
I have bound the TemplateColumns in Codebehind in RowDataBound-Event f.e.:
Dim LblCustomer As Label = DirectCast(e.Row.FindControl("LblCustomer"), Label)
LblCustomer.Text = drCharge.ERP_CustomerRow.CustomerName 'drCharge inherits DataRow
What is the recommended way to sort a GridView on columns related to other tables? I could build a custom datatable with the customername instead of the foreignkey and bind this column to the TemplateField. But then my huge dataset in the model makes no sense anymore.
EDIT:
It seems that my question was not clear or too special.
Perhaps i can rephrase it in a more general term.
I have a model with a Dataset. I'm binding one Datatable(ERP_Charge) from it to my GridView(actually i take a Dataview from that Table). In this Datatable are columns that are related to other Datatables in the Dataset(relations are defined). When i want to make the grid sortable its no problem on the columns that belong to ERP_Charge. But the columns with foreign keys to other table could not be sorted because the Gridview shows f.e. not the CustomerID but the Customername. I get the Customername in RowDataBound.
Normally i would join the tables and add a Datacolumn for the Customername. Then i set this "virtual" Datatable as Datasource from the Gridview and i'm able to sort it. But i didnt want to create the datatables in the Page on the fly(it belongs into the model). Do i have to define it in the dataset-Designer? I thought it would be sufficient to define the relationships in the dataset.
UPDATE: i had solved my sorting problems as below.
Regards
UPDATE: i had solved my sorting problems in the following way: I changed my DataSet in the model and joined the related table to the Main-Table(ERP_Charge). I added the columns to the Datatable(f.e. CustomerName) that i want to sort. But i did that without saving it in the configuration wizard of the DataAdapter, because that would delete all Update-,Delete- and InsertCommands. Hence i changed only the SelectCommand with the one that was generated from the Wizard. Therefore i kept the benefit of my Datamodel with the ability to sort for columns of related tables. Maybe queries are a little slower now because of the joins but it works for me.
Related
Please if someone have an idea about what i'm searching about.
i'm develloping an application web with asp.net and sql server. I have used a gridview and i want to make every new user after login see the gridview clear in order to create its own values.
But The gridview must still related with the same table of the database.
I hope that you understand my problem and i'm sorry for my english.
Are the columns in the gird bound to columns of your users table or are they bound to it's own separate table?
If it's the latter, the table for the grid should have a column specifically for the users' id
for example, if the records for the gridview were held in table USER_GRIDVIEW_RECORDS:
ALTER TABLE USER_GRIDVIEW_RECORDS
ADD USER_ID INT NOT NULL;
This way you can set the DataSource of the grid to the result of the following query:
SELECT * FROM USER_GRIDVIEW_RECORDS
WHERE USER_ID = LOGED_IN_USER_ID
And write the records of the gridview back to the database with the following query.
INSERT INTO USER_GRIDVIEW_RECORDS (USER_ID, OTHER_COLUMN_NAMES, MORE_COLUMN_NAMES)
VALUES (LOGED_IN_USER_ID, 'Example value', 'another example')
If this isn't enough for you, give us some example code of how you query your server and about how you write the DataSet to the DataSource of your GridView
I'm using a huge datatable for a survey analysis. Instead of calling SQL server frequently am using row filtering on this datatable using DataView.
But the problem is once I filter the DataTable by
dv= dt.defaultview
dv.rowfilter="id=10"
that works fine. Dv having a filtered rows
But when we assign the same dt to another view, the Main datatables Default view being changed based on the filteration done for DV which is ID=10
So in the immediate window I checked this
dt.defaultview.totable().rows.count
and its 20 (WHich was because of the filteration done for DV)
but
dt.rows.count
is 1500 which is the correct count.
But my question is how this happened since I didnt touched the Datatable and I assigned the filter for DV only. Even then how the defaultview of the datatable changed?
"Even then how the defaultview of the datatable changed?" Isn't that obvious if you look at the first two lines of code you have posted?
dv = dt.DefaultView
dv.rowfilter = "id=10"
If you want to remove that filter on the DefaultView you have to assign empty string or Nothing:
dt.DefaultView.RowFilter = ""
i have a dataset which contains three tables dt1, dt2, and dt3 (in the sequence), now i am maintaining the viewstate for every table, to retain the table across postback, (i.e i have latest DataTable inside viewstate), now the problem is i want to apply ViewState of dt3 to the respective table in DataSet, i.e dt3, i am doing below:
dataSet.Tables[(it will be dynamically fetched, say for here its) 0] = ViewState["DataTable" + (it will be dynamically fetched, say for here its) 0]
but here i am getting error saying dataSet.Tables[] is read-only.
can any one help how shall i go about at. Let me know if anyone needs more information about my question.
You should use
DataSet ds = new DataSet();
ds.Tables.Add(YourDataTable);
OR
ds.Tables.AddRange(YourDataTableArray);
To update an existing table in ds from ViewState you can try two approaches.
Remove Table from DataSet by using DataSet.Tables.Remove("Table"); method, Note that you should not rely on index approach to fetch Tables from DataSet instead you should provide your Tables a unique name so that you can fetch and remove them when you want to.
You can use loop approach to fetch rows from your ViewState DataTable and then insert them into DataSet's Table one by one, Something like this.
ds.Tables[0].Rows.Clear();
DataTable dtFromViewState = ViewState["dt"] as DataTable;
foreach (DataRow row in dtFromViewState.Rows)
ds.Tables[0].Rows.Add(row.ItemArray);
SQL Tables
Listing
ID, Title.....
ListingType
ID, Name
ListingMatrix
ListingID, ListingTypeID
Basically a listing can be more than 1 type and I want that to be able to be shown using the ListingMatrix table. However, I'm having a lot of issues populating the checkboxlist because I have it being sorted by Title to keep it user friendly. I'm using VB.Net, LINQ and MS SQL.
Dim readListingMatrix = (From ListingCategories In db.ListingTypeMatrixes _
Where ListingCategories.ListingID = ListingID)
For Each row In readListingMatrix
CheckBoxListListingCategories.Items(row.ListingTypeID - 1).Selected = True
Next
My issue is storing the checklistbox and editing it. Storing I think I could hack, but editing it is becoming a pain since I can't get the checkboxlist to check the correct boxes since their location changes due to the ORDER BY in my SQL Statement that populates the list.
Assuming that the value field of your checkboxes is filled with a ListingTypeID, do this:
Dim readListingMatrix = (From ListingCategories In db.ListingTypeMatrixes _
Where ListingCategories.ListingID = ListingID)
For Each row In readListingMatrix
CheckBoxListListingCategories.Items.FindByValue(row.ListingTypeID).Selected = True
Next
I have a very simple ASP.Net page that acts as a front end for a stored procedure. It just runs the procedure and shows the output using a gridview control: less than 40 lines of total code, including aspx markup. The stored procedure itself is very... volatile. It's used for a number of purposes and the output format changes regularly.
The whole thing works great, because the gridview control doesn't really need to care what columns the stored procedure returns: it just shows them on the page, which is exactly what I want.
However, the database this runs against has a number of datetime columns all over the place where the time portion isn't really important- it's always zeroed out. What I would like to be able to do is control the formatting of just the datetime columns in the gridview, without ever knowing precisely which columns those will be. Any time a column in the results has a datetime type, just apply a given format string that will trim off the time component.
I know I could convert to a varchar at the database, but I'd really don't want to have to make developers care about formatting in the query and this belongs at the presentation level anyway. Any other ideas?
Finally got this working in an acceptable (or at least improved) way using this code:
Protected Sub OnRowDatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim d As DateTime
For Each cell As TableCell In e.Row.Cells
If Date.TryParse(cell.Text, d) AndAlso d.TimeOfDay.Ticks = 0 Then
cell.Text = d.ToShortDateString()
End If
Next cell
End If
End Sub
If you are auto generating the columns which it sounds like you are. The procedure for using the grids formatting is awful.
You would need to loop through all the columns of the grid, probably in the databound event and apply a formatting expression to any column you find is a date column.
If you are not auto generating and you are hadcoding columns in your grid you will also know alreayd which columns are date columns and you can apply the same format expression to that column. It's something like {0:ddMMyyyy} but you will have to look it up as that's probably not quite right.
so to summarise hook into the databound event. loop through the column collection and ascertain if the column is a date column. I wonder how you might do this :). If you decide a column is a date column set its format expression.
Voila
---------------------- EDIT
Ok how about you write you method that returns the data from the proc to return a datatable. You can bind the datatable to your grid after formatting the data in the datatable. The datatable.Columns collection is a colection of DataColumns and these have a DataType property. You may be looking for System.DateTime or DateTime and it may be one of the properties of the DataType property itself :). I know it's cumbersome but what you are asking is definitly going to be cumbersome. Once you've identified date columns you may be able to do something with it.
If not i'd start looking at the data readers and see if there's anything you can do there or with data adapters. I wish I could give you a proper answer but i think however you manage to do it, it's not going to be pretty. Sorry
if using explicit bound columns is an option, add a DataFormatString to your boundField
<asp:BoundField DataField="Whatever" ... DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="False"/ >
otherwise you could look at doing the formatting the GridView.OnRowDataBound event
You can use the isDate() function to see if something is a valid date and then use dateformatting options to make it look like you want.
Some examples for date formating:
http://datawebcontrols.com/faqs/CustomizingAppearance/FormatDateTimeData.shtml