Binding repeater to a record? - asp.net

I need to bind repeater with some structure that has only a record. Data table is already populated. I tried to bind it with a datarow or datarowview but it do not allow to do. Any suggestion please ?
Can I use Linq query for that ? If so can you please guide me the query for selecting first row of data table ?

Try this
Dim drList As New List(Of DataRow)
drList.Add(myDataTable.Rows(0)) 'add the first row in the datatable
myRepeater.DataSource = drList

Related

Get DataRow from the DropDownList

I doesn't know how to convert the dropdownlist selected item to datarow
sDropDownList.DataSource = GetTable();
sDropDownList.DataTextField = "Name";
sDropDownList.DataValueField = "ID";
I tried to get the datarow from the dropdownlist like this.. .
DataRow row = (sDropDownList.SelectedItem as DataRowView).Row;
But i get an error .. . errors like Can't convert listitems to datarows
It's not a DataRow on postback. A DropDownList's item has a Text and a Value since it's a ListItem. If you need the other informations from the record later (on postback) you have to query the database again.
Actually you have all you need since you've set the DataValueField to the ID-column.
int id = int.Parse(sDropDownList.SelectedValue);
// now query the database to get the whole record or the information you need

Dropdownlist populated with Linq to SQL not displaying items correctly - VB

I am populating a dropdownlist in the edititemtemplate of a formview using linq to SQL.
I am getting the data with the below code:
Dim wdc As New WeeklyChecksDataContext
Dim mustchk = (From w In wdc.WeeklyChecks
Where w.DateStamp = Request.QueryString(0)
Select w.musterCheck).FirstOrDefault()
When debugging I can see that the value "Issue" is being assigned to the mustchk variable which is correct.
I am then databinding the dropdownlist as below:
cbMusterReport.DataSource = mustchk
cbMusterReport.DataBind()
When running the web page the value "Issue" is databound to the dropdownlist but each letter of the word "Issue" is databound to its own separate item rather than the word "Issue" being databound as the only item in the dropdownlist. Can't work out what I am missing here. Thanks
You need to put the string in a collection. Try this:
cbMusterReport.DataSource = new String(){ mustchk }
cbMusterReport.DataBind()
Sorry, I've worked it out. Can do it like this
cbMusterReport.Items.Insert(0, New ListItem(mustchk, mustchk))

Select Row from GridView

how i can select Row from GridView without DataKey ?
I have a Grid view with data source from List and i need when
press select select Get the selected row ?
List<string> RequestedMovie = new List<string>();
GridView3.Datasource = RequestedMovie;
You can directly get the selected row by using the following line of code
GridViewRow objRow = GridView3.SelectedRow;
..
..
I hope that will resolve your problem.

Is it possible to do a linq query on a GridView (ASP.NET)?

Basically I have a datakey that I'd like to query from my GridView instead of looping through all the rows and comparing the key of each row. So I was wondering if it was possible to just do a linq query on the gridview (not datatable) and filter with the datakey.
Not sure how to use DataKeyNames directly, because Column doesn't have any information about data field name it's coming from. In the example below, I use SortExpression to get column index which is used for filtering.
EDIT: The most important part here is the casting, which enables you to use all the fancy extension methods designed for IEnumerable<T>.
int idColumnIndex = MyGrid.Columns.Cast<DataControlField>().Where(e => e.SortExpression == "ID").Select(e => MyGrid.Columns.IndexOf(e)).FirstOrDefault();
var row = MyGrid.Rows.Cast<GridViewRow>().Where(e => e.Cells[idColumnIndex].Text == "421").FirstOrDefault();
Everything is possible!
Gridview in itself is nothing. It's just a UI, the data is in its source be it a datatable or dataset and you can use linq to query them.
As far as I understand the theory of LINQ, it can be performed on any list. As a datasource in a gridview is in essence a list, you should be able to use LINQ on that datasource of the gridview.
Try this example:
http://weblogs.asp.net/scottgu/archive/2006/05/14/Using-LINQ-with-ASP.NET-_2800_Part-1_2900_.aspx
I did something similar to this with a repeater, and maybe it will help... or you can just disregard it if it doesn't. In my situation I databound the repeater and allowed users to modify the data before exporting it to XML. The following LINQ loops through each row in the repeater, uses findcontrol to grab the control from the datarow and then uses Linq to XML, but this could be used to generate objects using LINQ to Objects. rp is the repeater, cbIgnore is a checkbox which if the users checks it the row is not expored.
Dim doc As New XDocument( _
New XDeclaration("1.0", "ISO-8859-1", "true"), _
New XElement("Schedule_Import", _
From c As RepeaterItem In rp.Items _
Where (c.ItemType = ListItemType.Item Or c.ItemType = ListItemType.AlternatingItem) _
AndAlso DirectCast(c.FindControl("cbIgnore"), HtmlInputCheckBox).Checked = False _
Select New XElement("activity", _
New XElement("code", DirectCast(c.FindControl("txtAC"), TextBox).Text), _
New XElement("starttime", DirectCast(c.FindControl("dtfStart"), DateTimeField).SelectedDateTime), _
New XElement("endtime", DirectCast(c.FindControl("dtfEnd"), DateTimeField).SelectedDateTime), _
New XElement("description", DirectCast(c.FindControl("txtTitle"), TextBox).Text))))

Error in DropDownList using ASP.NET

I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.
So I did this :
DDL.DataSource = myDataReader
DDL.DataBind()
But it's giving me (5 records) "the number of records of the table" but like this :
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
You should set DataTextField and DataValueField, otherwise data binding will perform .ToString() on every row and put it as item:
DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();
you have to set the text and the key fields of the ddl before you databind
DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
The code : ddl.datasource=reader is just setting the content present in reader (array of columns of table) as the main source of data.Now as ddl show only a single column in it so u need to write a piece of code which tells ddl that which column it has to display.So you will write: ddl.textfield="column name which you want to show";
and ddl.valuefield="column name which you want as a reference to pass to database";

Resources