C# - Passing LINQ DataRow Reference In GridView's ItemTemplate
Mar 25, 2010
SOLVED. Code has been edited to reflect solution. Given the following GridView:
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false" DataKeyNames="UniqueID"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
<Columns>
<asp:BoundField HeaderText="Remarks" DataField="Remarks" />
<asp:TemplateField HeaderText="Listing">
<ItemTemplate>
<%# ShowListingTitle( Container.DataItem ) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Amount" DataField="Amount" DataFormatString="{0:C}" />
</Columns>
</asp:GridView>
which refers to the following code-behind method:
protected String ShowListingTitle( object /* was DataRow */ row )
{
Listing listing = ( Listing ) row;
return NicelyFormattedString( listing.field1, listing.field2, ... );
}
The cast from DataRow to Listing was failing (cannot convert from DataRow to Listing) I'm certain the problem lies in what I'm passing from within the ItemTemplate, which is simply not the right reference to the current record from the LINQ to SQL data set that I've created, which looks like this:
private void PopulateGrid()
{
using ( MyDataContext context = new MyDataContext() )
{
IQueryable < Listing > listings = from l in context.Listings where l.AccountID == myAccountID select l;
GridView1.DataSource = listings;
GridView1.DataBind();
}
}
View 3 Replies
Similar Messages:
Jan 21, 2011
I am using a template file for the ItemTemplate of the repeater control.
Within the template I am trying to get the value from the current datarow.
The code was then converted from VB to C# and I am getting the following error:
'System.Web.UI.Control' does not contain a definition for 'DataItem'
Below are the C# and VB versions:
[Code]....
Converted C#:
In .ascx...
<%#GetAssociationName((RepeaterItem)Container.DataItem)%>
In .ascx.cs...
Then get the value using the following method.
[Code]....
View 1 Replies
Dec 21, 2010
How can i convert following sql guery to LINQ in C# ? I dont need all columns from both tables and the result set should be IEnumerable<DataRow>
select c.level,cl.name,c.quantity
from dbo.vw_categories c
left join dbo.vw_categoriesLocalization cl on c.level = cl.level and cl.language = 'en'
where c.level like '000%' and c.level <> '000';
View 2 Replies
Mar 28, 2010
I have taken a DataTable control and have added colums to it. Now i want to add rows to that DataTable but when i am trying to create a DataRow of DataTable, an error is generated, showing "Value of type 'System.Data.DataRow' cannot be converted to 'dataRow'".
exact coding that i have written is as follows:
[Code]....
So, where i am getting wrong?
View 2 Replies
Feb 8, 2010
I'm trying to programatically reference a FileUpload control which I have within an ItemTemplate. I get a 'Name FileUpload1 is not declared' error message. Code below:
Code is as follows:
[Code]....
[Code]....
View 7 Replies
Jan 17, 2011
I have something like this:
[Code]....
[Code]....
This does NOT work. What's wrong here?How do I accomplish my goal? I MUST use the Button1_Click event for this one.
View 13 Replies
Apr 1, 2010
I have a gridview with the columns below. Since Intern comment is going to be too long to fit it on the same line. I wanted to create a second row during databind and rowdatabound to display the Interncomments on the second line. But How can I put the comments inside a Literal control ?
Which will look like this
[Code]....
View 8 Replies
Jan 7, 2010
I am facing a problem with GridView DataRow Width (defined by CSS) in FireFox.I have a GridView which has headers and items which their width is defined in CSS file.When I test its layout in FireFox and IE 8.0 I got a problem about width.- Headers in 2 web browsers : OK (the same)- Items : In Firefox, the items' width is wider than headers' width so the row is broken.Here is its CSS and render code :
[Code]....
View 2 Replies
Feb 18, 2011
I'm implenting a functionality of inbox where i have a GridView and code as follows:
[code]....
But here even if i select a checkbox onclick event gets fired. i dont want the checkbox to fire this event. It should just act for selection of rows. What shall I do?
View 1 Replies
May 8, 2013
How to bind generic list of DataRow to gridView...
I am using MVC arch , in classDAO m fetching data from table and in bussiness layer ,storing datatable into list<DataRow> and finally in controller , i want to bind in gridview..how can i read each datarow value and bind to gridview..
View 1 Replies
Sep 7, 2010
I need to change the row.forecolor = drawing.color.red.
in my gridview i have a STATUS field, its ItemTemplate is LABEL1 and its EditTemplate is DropDownList1.
on loading I have successfully changed the forecolor, but when I edit it gave me an error:
Object reference not set to an instance of an object.
View 7 Replies
Jan 24, 2011
I am working on asp.net application and code behind is c#
I am having gridview and adding the data to the gridview in the following manner.
[Code]....
Now i want to add <div> element like the one below after the table row <tr> in the generated html
[Code]....
View 4 Replies
Aug 1, 2010
new to asp.net and mvc, sorry if the question sounds dumb. I currently have a model (a Linq type) that get data from a table. I use this model to do a Linq qery and display all records in a listing table.I would like to do exactly the same but using a SQL command/query rather than Linq as I'm much more confortable with "standart" SQL. I now need to join tables and return specific columns with custom names as some columns have the same name in the join of my query.If you have another idea (create a stored procedure to be in control of the SQL and then use Linq to call it, for example), I'm open to sugestions.In the view, I loop all records doing: foreach (var item in Model) {}Not sure if this could be important o note
View 2 Replies
Aug 24, 2010
I am taking a table (Articles) and grouping all the results by the key that references another table (ArticleSectionCategories). I then want to sort these results by the OrderBy field of the referenced table (ArticleSectionCategory.OrderBy). This is what I am using right now but it is not working.
[Code]....
View 2 Replies
Jan 20, 2010
im new to linq and im having lots of trouble creating complex queries.. My problem mainly is that i am using Linq as a BLL.
i created a dbml (with namespace called DAL) file with 2 tables with a one to many relation. Pop and City. There is a forgien key in pop that points to a city. I also created a class called Pop that inhertis DAL.Pop. Now in my ASPX page, i want to call a function in the Pop class that would retrieve some Pop according to its ID, but i need the city name as well.. Ive been going around this in circles for days. I dont know how to write the Linq syntax and i dont understandd how my function's signature should be like.. I want to recieve a table that i can bind to a Gridview and preferable, specify which coloumns to bind.
View 2 Replies
Dec 7, 2010
Trying to pass in my DataTable to a stored proc using LINQ. Below is my code.
var sqlCommand = new System.Data.SqlClient.SqlCommand {
CommandType = System.Data.CommandType.StoredProcedure,
CommandText = "UserIdList"
};
var dataTable = new System.Data.DataTable("IdList");
dataTable.Columns.Add("AttributeIds", typeof(Int32));
dataTable.Rows.Add(26);
dataTable.Rows.Add(40);
dataTable.Rows.Add(41);
dataTable.Rows.Add(45);
dataTable.Rows.Add(78);
dataTable.Rows.Add(33);
dataTable.Rows.Add(36);
//The parameter for the SP must be of SqlDbType.Structured
var parameter = new System.Data.SqlClient.SqlParameter {
ParameterName = "@AttributeIds",
SqlDbType = System.Data.SqlDbType.Structured,
TypeName = "ecs.IDList",
Value = dataTable,
};
sqlCommand.Parameters.Add(parameter);
var user = myDC.DC.ExecuteQuery("exec ecs.udpUserAttributeDetails {0}, {1}", sqlCommand, userId).SingleOrDefault();
View 1 Replies
Mar 31, 2010
I have a SP db.sp_GetProfitMatrix , it takes nullable integer parameters, for which I have a default . It refuses me when I try to pass a null value (the two date parameters are nullable in the Linq designer) I get {"Nullable object must have a value."} error. here is my code:
int? nlDt = null;
var pms =
from p
in db.sp_GetProfitMatrix( nlDt.Value, nlDt.Value)
select
new
{
p.Volume,
p.GrossSales,
p.NetSales,
p.COGS
};
View 4 Replies
Aug 15, 2010
I have a user control with a datalist. I just want to do some simple binding but keep getting an object reference not set to an instance of an object error. The following is the code, I am sure I am just missing something simple:
Dim dc As Forms.DataClassesDataContext = New Forms.DataClassesDataContext
Dim formQuery = From fq In dc.v_form_questions _
Where fq.FormTypeID = FormTypeID _
[code]...
When I debug, I see values for the formQuery; I am also able to loop through the formQuery and response.write a field out, however every time I try to bind it to a databound control, I get the Object reference not set to an instance of an object error.I initally was using a simple LinqDataSource object, but needed to set the where parameter to the FormTypeID property of this usercontrol. I had a Where condition defined on Page Init, but started getting the object reference error.
View 1 Replies
Jul 9, 2010
Now I have a datalist
I have done the itemtemplate and edittemplate,when I click on the button in itemtemplate, the edittemplate will show.But that row in itemtemplate will disappear.How can I keep that row in itemtemplate and ecotent in edittemplate both appear?
View 4 Replies
Aug 21, 2010
I usually work with windows forms, and am just starting to work with ASP.Net, so appologies if this is a dumb question!
I have a master page which has a search button on it. The results of this are built up using a Linq statement and if there are any results, I want to display the results in a contents page.
On the masterpage, I have the following code:
[Code]....
On the SearchResults.aspx page I have this code.
[Code]....
m.searchRes is always nothing.
View 2 Replies
Feb 23, 2010
I have an application where depending on what the user selects run a different stored procedure.
I am using LINQ and am passing the stored procedure name into a sub that will run the stored procedure and return values that I am loading into a DDL.
For example I want to pass the procedure name "GetProducts_Hats" to the sub and not hardcode it.
Protected Sub LoadParamDropDowns(
ByVal SqlObjToExec
As
String )
I am passing the storedprocedure name in parameter SqlObjToExec
so instead of
values = mydb.GetProducts_Hats
I want to code it to be a variable
values = mydb.SqlObjToExec
But the above code does not work.
View 7 Replies
Feb 20, 2010
I have downloaded Visual Web Developer 2010 Express and am trying to use LINQ. In order to do that I have added a LINQ to SQL class to the web site and have dragged tables from the database view into the LINQ designer, which worked fine.But..When I add: using System.Data.Linq;to MainPage.xaml.cs, I receive an error:
Error 3 The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)Even although I have references to System.Core, System.Data, System.Data.Linq in the website references.
View 4 Replies
Jul 29, 2010
I have two item template which consist two dropdownlist D1,D2...
here what im doing is d2 is binding based d1 selected value...
im using this code in D1_Selectedindexchanged
foreach (GridViewRow row in Gridview1.Rows)
View 21 Replies
Aug 19, 2010
I have one dropdownlist in gridview itemtemplate and one textbox out side of the gridview..What my problem is i have written this query to bind drodownlist.. but itz not working.. Select Name from tabel1 where Type in (@Typ) Here @Typ is my Textbox values let us consider in my textbox i have A1','A2','A3 then itz not showing me the Name in dropdownlist of this three Type ..?
View 2 Replies
Feb 7, 2010
I'm trying to add a LINQ datasource to my ListView control but it doesn't pick up the data context that I created. All it shows as options in the dropdown list are AJAX objects. What could be the possible causes for this?
View 3 Replies