|
Hi,
I have the following linq code within my app
var results = (from t in query
where t.ProjectID == new Guid(this.Session["ProjectID"].ToString())
orderby t.Number ascending
select new
{
ID = t.ID,
Tag = t.Number,
Description = t.Description,
SubSystems = GeneralHelper.TagSubSystems(t),
Module = t.Area.Module.Name,
Discipline = t.Discipline.Name,
ITRs = GeneralHelper.TagITRsHTML(t)
});
I want to pass the query result to another function to handle my paging. I currently have another function defined like this
public void BindGridData(IQueryable result)
{
gridView.DataSource = result;
gridView.DataBind();
}
It works like this, but the problem is I cannot do "take" or "skip" on "result" because it's declared as IQueryable. Is there a work around to handle this?
Thanks
|
|
|
|
|
convert the linq result to list
var results = (from t in query
where t.ProjectID == new Guid(this.Session["ProjectID"].ToString())
orderby t.Number ascending
select new
{
ID = t.ID,
Tag = t.Number,
Description = t.Description,
SubSystems = GeneralHelper.TagSubSystems(t),
Module = t.Area.Module.Name,
Discipline = t.Discipline.Name,
ITRs = GeneralHelper.TagITRsHTML(t)
}).ToList();
|
|
|
|
|
If I convert it to list, then how do I define the function that takes the query result?
|
|
|
|
|
public void BindGridData(IEnumerable result)
{
gridView.DataSource = result;
gridView.DataBind();
}
Also note the use of the pre tags to properly format the code snippet
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
IEnumerable requires an argument,
IEnumerable result
any idea what argument would work?
|
|
|
|
|
Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is.
Alternatively, make your Bind function generic, like so:
public void BindGridData<T>(IEnumerable<T> result)
{
gridView.DataSource = result;
gridView.DataBind();
}
|
|
|
|
|
|
Although I agree a concrete type is better for readability and good design it isn't necessary in order to pass the query to a method.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
the reason i didnt define a concrete type is because the function will be accepting queries from different classes
|
|
|
|
|
Ok, then the second solution is best
Oh, and you don't need to do a ToList() if you're going that route. Linq results are all IEnumerable.
|
|
|
|
|
Using generics, as Ian has suggested, doesn't preclude you from doing that. You would just need to create the class/struct for each query, which from a architectural perspective isn't a bad idea. However, if you have many different queries that are possible then perhaps you need to improve your design, one size fits all implementation are very difficult to maintain.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
One more thing,
lets say I store "result" in session variable like this
public void BindGridData<t>(IEnumerable<t> result)
{
Session["result"] = result;
gridView.DataSource = result;
gridView.DataBind();
}
and then I want to call this function within another event, how do I call it? how do I cast Session["result"]?
Thanks
|
|
|
|
|
First of all don't store it in session state. Remember this is the result of your query, not the query itself, and could be very large.
It's also quite clear how to cast it. It is being passed into your method as an IEnumerable, guess what it should be cast as?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
IEnumerable<T> requires a type but IEnumerable does not.
I know the language. I've read a book. - _Madmatt
modified on Tuesday, August 31, 2010 12:12 PM
|
|
|
|
|
|
Sorry, the brackets didn't render. Fix it
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Hi all, I'm hoping someone could shed some light here please.
I have a ToolStripPanel on my form with a few ToolStrip s which in turn each have a few ToolStripButton s.
I wrote an event handler for the MouseEnter and MouseLeave events of all of these buttons so that I can change the image depending on whether the mouse is over the button or not. Basically, I have an animated gif which I'm showing when the mouse is over the button and a static image when it's not.
It works but it has a little glitch. Every time the image of a button changes, the size of the button changes very briefly. This means that, for a split second, the relevant ToolStrip is resized which has the effect of all the ToolStrips to the right of it being moved left. Like I say, it's only for a split second before everything is restored again (once the button has its new image) but it makes for a very unpleasant visual effect.
Any ideas?
|
|
|
|
|
Hi,
Could you post the code you're using to change the images on your buttons ? This way we would have a better idea on how you handle the stuff.
|
|
|
|
|
Sure, I have two functions:
private void toolStripButton_MouseEnter(object sender, EventArgs e)
{
switch ((sender as ToolStripItem).Name)
{
case "toolStripButton_ViewUser": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_anim_User; break;
case "toolStripButton_NewUser": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_anim_UserAdd; break;
case "toolStripButton_ViewItems": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_anim_Item; break;
case "toolStripButton_NewItems": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_anim_ItemAdd; break;
... etc.
}
}
private void toolStripButton_MouseLeave(object sender, EventArgs e)
{
switch ((sender as ToolStripItem).Name)
{
case "toolStripButton_ViewUser": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_User; break;
case "toolStripButton_NewUser": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_UserAdd; break;
case "toolStripButton_ViewItems": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_Item; break;
case "toolStripButton_NewItems": (sender as ToolStripButton).Image = Properties.Resources.BtnIcon_ItemAdd; break;
... etc.
}
}
The MouseEnter and MouseLeave events of all the buttons are handled by these two functions.
|
|
|
|
|
You can try to encapsulate switch statements between this.SuspendLayout() and this.ResumeLayout() , in order to stop rendering your form while you're changing your images.
You can also try to set the ImageScaling property of your ToolStripButtons to None ; but only if all your images have the same size, and you have set the ImageScalingSize property of your ToolStrip to the actual size of your images.
I don't have VS installed on this machine, so I was not able to test for these solutions. I hope it will help you anyway.
|
|
|
|
|
Thanks a mil. I had considered the ImageScaling option but that wouldn't work because the images are in fact bigger than the buttons and have to be scaled. The SuspendLayout() and ResumeLayout() suggestion worked like a charm though, thanks for that.
|
|
|
|
|
You're welcome
|
|
|
|
|
Hi,
I'm using Tab Control in my application, where the text fore color is black and backcolor is white. But I would like to have fore color as white and backcolor as black. If I change "DrawMode" property to OwnerDrawFixed, I'm able to change the style. But its not showing the changed styles in XP machine. Its working in Vista machine.
Please suggest me other way to have text color as white and backcolor as black.
Thanks in advance.
|
|
|
|
|
Hi,
ForeColor and BackColor are properties of TabPages, not of TabControl itself.
Try to reset the value of TabControl.DrawMode to Normal , and set the BackColor and ForeColor properties on the TabPages.
Regards.
|
|
|
|
|
Thanks for your reply. I reset the value of TabControl.DrawMode to Normal and set the BackColor and ForeColor properties on the TabPages.
<pre>const string DEFAULT_TAB_NAME = "Default";
tabCtrl1.TabPages.Add(DEFAULT_TAB_NAME, DEFAULT_TAB_NAME);
tabCtrl1.TabPages[DEFAULT_TAB_NAME].Controls.Add(dataGridView1);
tabCtrl1.TabPages[DEFAULT_TAB_NAME].ForeColor = Color.White;
tabCtrl1.TabPages[DEFAULT_TAB_NAME].BackColor = Color.Black;</pre>
But the tabname, "Default", characters are not appearing in white color and back color not in black color. Had I followed you?
|
|
|
|