This is a migrated thread and some comments may be shown as answers.

How to filter the results based on a column value

5 Answers 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stavros
Top achievements
Rank 1
Stavros asked on 14 Nov 2008, 08:41 AM
I populate the RadGrid when I setup the connection where I use a SELECT column1, column2 etc.
One of the columns is the id of the loged in user.
This column is used in the SELECT command but I use Display="false" attribute in the GridBoundColumn tag so that it is not displayed.
How can I filter the results in the background so that I can see only the rows with a specific id?

I can get the id of the loged in user with this code :

MembershipUser

 

myObject = Membership.GetUser();

 

 

 

string UserID = myObject.ProviderUserKey.ToString();

 

 

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Nov 2008, 11:05 AM
Hello Stavros,

You can set the DataKeyNames as the UserId column(say column1) and then check if the UserId matches the DataKeyValues for the row and according show/hide rows. Check out the code below for a clear picture of the same.
aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource2" OnPreRender="RadGrid1_PreRender" >        
   <MasterTableView DataSourceID="SqlDataSource2" DataKeyNames="UserID"
             

cs:
protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridDataItem data in RadGrid1.Items) 
        { 
            if (data.GetDataKeyValue("UserID").ToString() == UserID) 
            { 
                data.Visible = true;           
            } 
            else 
            { 
                data.Visible = false
            } 
        } 
    } 

Thanks
Princy.

0
Stavros
Top achievements
Rank 1
answered on 14 Nov 2008, 12:15 PM
I tried your solution by I get an error.
Here's what I tried :

 

protected

 

void RadGrid1_PreRender(object sender, EventArgs e)

 

{

 

MembershipUser myObject = Membership.GetUser();

 

 

string UserID = myObject.ProviderUserKey.ToString();

 

 

 

 

foreach (GridDataItem data in RadGrid1.Items)

 

{

 

if (data.GetDataKeyValue("UserID").ToString() == UserID)

 

{

data.Visible =

true;

 

}

 

else

 

{

data.Visible =

false;

 

}

}

}

  

and I get the following error:

UserID is neither a DataColumn nor a DataRelation for table DefaultView

 

 

0
Stavros
Top achievements
Rank 1
answered on 14 Nov 2008, 01:01 PM
It worked!
I forgot to change UserID with the column name I use in my database!
Thanx again for your help!
0
Stavros
Top achievements
Rank 1
answered on 16 Nov 2008, 09:40 PM

Is there any way to do the reverse thing?
What I mean is when a user adds a record his userid is added automatically to the record userid column.

Thank you very much for your immediate help!

0
Vlad
Telerik team
answered on 17 Nov 2008, 07:10 AM
Hello Stavros,

Please check this article for more info how to provide default values for insert:
http://www.telerik.com/help/aspnet/grid/apiforcontrolling_net2.html

Kind regards,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Stavros
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Stavros
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or