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

[Solved] Working with data bound columns

3 Answers 184 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Dunbar
Top achievements
Rank 2
Michael Dunbar asked on 08 May 2009, 08:31 AM
I am creating a grid programmatically:

RadGrid gridFileView = new RadGrid(); 
gridFileView.ID = gridFileViewID
gridFileView.Skin = "Vista"
gridFileView.AllowPaging = true
gridFileView.PageSize = 20
gridFileView.AllowSorting = true
gridFileView.ClientSettings.AllowRowsDragDrop = true
 gridFileView.ClientSettings.Selecting.AllowRowSelect = true
pnlFileView.Controls.Add(gridFileView); 
 
AddExistingFiles(gridFileView, docs); 


 and am creating the columns with the following code:

private static void AddExistingFiles(RadGrid gridFileView, ExternalDocuments docs) 
    { 
        // Bind files to grid 
        gridFileView.DataSource = docs
        gridFileView.AutoGenerateColumns = false
        gridFileView.MasterTableView.DataKeyNames = new string[] { "Guid" }; 
 
        AddGridRow(gridFileView); 
 
        gridFileView.DataBind(); 
    } 
 
    private static void AddGridRow(RadGrid gridFileView) 
    { 
        GridBoundColumn boundColumn; 
 
        boundColumn = new GridBoundColumn(); 
        boundColumn.DataField = "DocumentType"
        boundColumn.HeaderText = "Type"
        gridFileView.MasterTableView.Columns.Add(boundColumn); 
 
        boundColumn = new GridBoundColumn(); 
        boundColumn.DataField = "Uploaded"
        boundColumn.HeaderText = "Date added"
        gridFileView.MasterTableView.Columns.Add(boundColumn); 
 
        boundColumn = new GridBoundColumn(); 
        boundColumn.DataField = "Title"
        boundColumn.HeaderText = "Title"
        gridFileView.MasterTableView.Columns.Add(boundColumn); 
 
        boundColumn = new GridBoundColumn(); 
        boundColumn.DataField = "Filename"
        boundColumn.HeaderText = "File"
        gridFileView.MasterTableView.Columns.Add(boundColumn); 
 
        boundColumn = new GridBoundColumn(); 
        boundColumn.DataField = "Description"
        boundColumn.HeaderText = "Description"
        boundColumn.AllowSorting = false
        gridFileView.MasterTableView.Columns.Add(boundColumn); 
 
        boundColumn = new GridBoundColumn(); 
        boundColumn.DataField = "FileSize"
        boundColumn.HeaderText = "File size"
        boundColumn.AllowSorting = false
        gridFileView.MasterTableView.Columns.Add(boundColumn); 
    } 

I would like to get the DocumentType data bound column and replace the text with an image based on what that text is. Any ideas on how I go about approaching this?

Thanks,

Michael

3 Answers, 1 is accepted

Sort by
0
Michael Dunbar
Top achievements
Rank 2
answered on 08 May 2009, 12:21 PM
No suggestions anyone?
0
Shinu
Top achievements
Rank 2
answered on 11 May 2009, 05:08 AM
Hi Michael,

Try setting the UniqueName property for the Grid column. Access the Grid cells in the ItemDataBound event using the column's UniqueName and then add the image to grid cell depending on the text.

CS:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            string strtxt = item["DocumentType"].Text; 
            if (strtxt == "test"
            {  
                Image img = new Image(); 
                img.ID = "image1"
                img.ImageUrl = "Images/img1.gif"
                item["DocumentType"].Controls.Add(img); 
            } 
        } 
  } 


Thanks
Shinu
0
Michael Dunbar
Top achievements
Rank 2
answered on 11 May 2009, 07:42 AM
Excellent, thank you.
Tags
Grid
Asked by
Michael Dunbar
Top achievements
Rank 2
Answers by
Michael Dunbar
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or