.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

WebGrid: column definition - Set 'style:' parameters depending on value

Posted By:      Posted Date: May 22, 2011    Points: 0   Category :ASP.Net

I have a list of objects (List<myObject>) which I pass to a webGrid. I would like to set cells for a column of the table to a specific value depending the object's 'StatusColor' property. Something like

List<myObject> myObjectList = 
WebGrid webGrid = new WebGrid(source: myObjectList, ..);
columns: webGrid.Columns
webGrid.Column("Status", "Status", format: @<text>@item.StatusName</text>, style:



But for some reason, I get "cannot convert from 'lambda expression' to 'string'". When I remove the @<text> suround the item property, I get "the name '@item' does not exists in the current context"

For the 'format:' part, it works fine.

View Complete Post

More Related Resource Links

How to change image\parameters on style definition ?


I define some new button style.
The style default background is image X.bmp and when on 'mouseOver' is true the image is changing to Y.bmp

I have 10 buttons on the code that need to use this style - and each of the button need to show different image as default different image on 'mouseOver' event.

I can set the bitmap on each button on the button constructor - but i don't know how to set the image in this case on the style definition level.

Someone can help here ?

DataGrid: Tailor Your DataGrid Apps Using Table Style and Custom Column Style Objects


One of the most enduring challenges in writing user interfaces is figuring out how to display large amounts of data efficiently and intuitively without bewildering the user. The problem becomes particularly thorny when the interface must reflect hierarchical relationships within the data that the user needs to modify. The Windows Forms DataGrid control gives developers a powerful and flexible tool to meet this challenge. This article explains its basic operations and shows how to extend the DataGrid to display columns of data in an application-appropriate manner.

Kristy Saunders

MSDN Magazine August 2003

Filter items with a date column with parameters start date and end date on a sharepoint list?

If I have alist with a view that has the columns Title, Status and Status Date and the view has some items. How Can I add a filter with two parameters, start date and end date, to only show records with status date equal to or greater than parameter start date and status date is less than or equal to parameter end date? This is a sharepoint online site and I can't create and deploy custom code, can use SPD though. Do I need to use SPD or is this something I can do in the list settings? Thanks in advance.   Edit: I had a look here http://www.endusersharepoint.com/2009/09/29/sharepoint-date-filter-filtering-a-list-by-greater-than-or-equal-to-date/ but I don't have the Date Filter web part.

How to pass page column value to data view parameters

Hi All, I have a page library with custom column, lets call it "FormCourseId". On that page I have data view which retrieves data from custom list filtered by page column "FormCourseId". In parameter wizard creates the following tag <ParameterBinding Name="ListCourseId" Location="Form(FormCourseId)" DefaultValue=""/> Then I try to use that parameter in XSLT of data view as a filter creteria.  <xsl:param name="CourseId"></xsl:param> It does not work for me. If I change data view parameter source from Form to Query String and pass parameter as url one it works fine. What is wrong with page column parameter binding? May be if FormCourseId column is not presented in form layout as a field(I do not need to show that field in user form of the page) it could not be treated as form parameter?

Wierd case of: Msg 213, Column name or number of supplied values does not match table definition

Hi, I'm working on several triggers (that happen after insert or update) in order to log the changes in a different table. They all follow a similar syntax and are working fine, except for this one... I've reduced the next code to the minimum that gives an error, so we can safely assume the other parts of the trigger are working fine. INSERT INTO [Adt].[WardUnitStayLog] SELECT t.* FROM [Adt].[WardUnitStay] t INNER JOIN inserted i ON i.[Id] = t.[Id]; I've used this same syntax (but on different tables) in other triggers, and these are working perfectly fine. The above query provides the next error: Column name or number of supplied values does not match table definition. I've checked both tables for differences in the columns, but to no avail... (I've checked them manually and by outerjoining the information_schema.columns) (I've also checked the order in wich these columns are defined, they match over the two tables) These are the creation scripts for the tables: CREATE TABLE [Adt].[WardUnitStay] ( [Id] [dbo].[Id] IDENTITY(1,1) NOT NULL, [UnifiedUnitStayId] [dbo].[Id] NOT NULL, [WardCd] [dbo].[Cd] NOT NULL, [_FirstAtTm] [dbo].[Dtm] NOT NULL, [_IsReservation] BIT NOT NULL, [_LastAtTm] [dbo].[Dtm] NOT NULL, [_LastBedCd] [dbo].[Cd] NULL, [_LastPhysicianUid]

Schema comparer mistake in table-valued function (Nullability in column definition)

Hello. In my database i have table valued function. In its definition nullability of column is not set directly. This column has a user-defined data type. So the question is: is this column nullable? As i can see here: http://msdn.microsoft.com/en-us/library/ms174979.aspx   When column nullability is not explicitly specified, column nullability follows the rules shown in the following table. Column data type Rule Alias data type The Database Engine uses the nullability that is specified when the data type was created. To determine the default nullability of the data type, use sp_help. It should be not null, as user defined type is declared as not null. SMO says, that column is not null, but schema compare says it is nullable. Who is wrong and why?

Style depending on Windows Theme

Hi,   I want to style a ToolBar differently depending on which theme is used (Aero or Classic) in Windows 7. I have found some examples on the net but all describe how to do this on custom controls. I want to do this on a normal control. Is that possible?

Trying to apply style to a gridview column programatically..


I have a grid view that is bound from my code behind.   If the text of a label is equal to the value of a querystring, I want to apply a specific class/style that is in my .css.  I've tried this, but doesn't seem to have any affect...  How can I set the class for a column in the gridview if the two values match?   

 protected void gvMenu_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label lblSubSectionIDForComparing = e.Row.FindControl("lblSubSectionID") as Label;
            Label lblSubSectionNameForComparing = e.Row.FindControl("lblSubSectionName") as Label;

            if (lblSubSectionIDForComparing != null)
                if (lblSubSectionIDForComparing.Text == Request.QueryString["SubSectionID"])
                    e.Row.Cells[2].CssClass = "SubSectionNav";  <-- This is what I thought would work...


disable foreign key link depending on column value in the current row using Attributes


Hi ,

I want to disable  some foreign key links depending on column value in the current row

I am trying to have attribute like this :


 public partial class Order

        internal sealed class OrderMetadata
            public EntityCollection<Order_Detail> Order_Details { get; set; }

            public Nullable<DateTime> OrderDate { get; set; }



I had a look at Mr. Steve code for row highlighting , Conditional UIHint


SQLCommand parameters for column name



I want to insert column names as parameters into a SELECT statement (for MSSQL 2008): 

string sql = "SELECT @clm1 FROM db.tblUser WHERE aspUserId = @userId";
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlParameter parUser = comm.Parameters.Add("@userId", SqlDbType.NVarChar);
            SqlParameter parColumn= comm.Parameters.Add("@clm1", SqlDbType.NVarChar);
            parUser.Value = aspUserId;
            parColumn.Value = "clm1";
            string str = comm.ExecuteScalar().ToString();

and the ExecuteScalar returns the column name ("clm1") instead of the value.

But if I change the SELECT to "SELECT clm1 FROM db.tblUser WHERE aspUserId = @userId", ExecuteScalar returns the value of the "clm1" column (which is want I want!).

In what way can I insert column names into SQL statements as parameters? The purpose is to build dynamic SELECT commands...



ComboBox with Parameters & Column visibility


Dear All,

I have a report with many fields displaying in the report. I am checking the visibility of the columns.

When i am defining a separate parameter of boolean type iam able to hide the column visibility using the following xpression,



Now i have defined a parameter of

Data type = text, allows multiple values

Available values - in 'Specify Value' have specified the resulting column names in the Label and Value fields.

No Default Value.

So the parameter field holds all column names in a combobox with check box aside.

i am trying to hide the columns with the expression in the column visibility properties. but i am not able to use the same xpression, for it is only taking the parameter name in xpression but not the defined combobox values.


kindly help me with the formula..

I need to display the columns that are alone checked in the parameter combobox value.


Thanks !!

Column Alias Usage Everywhere in a Query Following Definition


In the following query, you cannot use the column aliases in WHERE and GROUP BY clauses, then suddenly like thunder from the blue, you can use it in the ORDER BY clause. It is counter-intuitive. It is counter-productive as well.

SELECT	[Year]=YEAR(OrderDate),
FROM AdventureWorks2008.Sales.SalesOrderHeader
WHERE YEAR(OrderDate) > 2002
GROUP BY YEAR(OrderDate), MONTH(OrderDate)
ORDER BY [Year],[Month]
/* Year	Month	TotalSales
2003	1	1968647.184
2003	2	3226056.1486
2003	3	2297692.9898

I made a suggestion to enable column alias usage everywhere in a query. You can vote for this feature at Connect:


Gridview column changing visibility depending on a row.text

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 if (e.Row.Cells[1].Text == null)
                GridView1.Columns[1].Visible = false;


and im not getting the result i expected =\


How can I pass parameters to my Business Data Column?

I have a custom list in which I have created a  Business Data Column that pulls data from a web service.  I have written the ADF file, it is installed on the server and I can get to the service's fields via the Business Data Type picker. However, and this is the part I can't figure out how to do: the web service requires 2 parameters be passed to it.  These parameters exist in other fields of the list item.  How do I make that association and pass those parameters?  Is this possible?

Also, I have not delved into using Visual Studio yet for SharePoint development, so I only have the UI and Designer as my tools.  I am on MOSS 2007.

Edit - I found this article: http://sharepointmagazine.net/technical/administration/everything-you-need-to-know-about-bdc-part-4-of-8.  Toward the bottom it explains how to do a 1:1 mapping of the list item to, in his example, the database item.  This is similar to what I need to do, except mine is a web service and it requires two parameters to identify it.

MVC 3 Beta WebGrid, T4MVC and Custom Column



I have the following MVC 3 Beta Grid:

  @grid.GetHtml(tableStyle: "Grid",  
                       columns: grid.Columns(  
                         grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Id })),
                         grid.Column(format: (item) => Html.ActionLink("Delete", MVC..User.Delete(item.Id)))

I am trying to solve three problems:

1 - Use T4MVC. On the last column, User.Delete, I get the following error:

     The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func<dynamic,object>, string, bool)' has some invalid  arguments

2 - Have the two ActionLinks, Edit and Delete on the same column, and have a CSS class for that column. Something like:

   grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Id }) + Html.ActionLink("Delete", MVC..User.Delete(item.Id)))

And make the CSS cl

Need an auto generating number for a document depending on a list column



I have an infopath form in my document library and I have list with columns Service ID and Service Name.

Base on the service id i need to generate another request id for the form starting from 0 for each service id as follows.

service 1---Id-1)Request form          

            ----Id-2)Request form  


service 2---Id-1)Request form

            ----Id-2)Request form  

Any little help would be much appreciated.



Using Html.Truncate in Webgrid Column

I'm using the Html.Truncate function from the MvcMusicStore tutorial (page 5) and I want to use it in a Webgrid column to trim some text. However I can't get the syntax correct to get the Html.Truncate function to work with a string in my Webgrid. My (now experimental) Webgrid looks like this:
@{ var grid = new WebGrid(Model, canPage: true, canSort: true); @grid.GetHtml(columns: grid.Columns( grid.Column("Comment", "Comment1", format: @<text>@Html.Truncate("Test string", 4)</text>), grid.Column("Comment", "Comment2", format: @<text>@Html.Truncate(@item.Comment, 8)</text>), grid.Column("Comment", "Comment3", format: @<text>@item.Comment</text>) )); }
 The code for column "Comment1" and "Comment3" is working but the code in "Comment2" is not working. The error message I get is
Description: An error occurred during the compilation of a resource required to service this request. 
Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.Helpers.WebGrid.Column(string, string
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend