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

Add ImageColumn at the end of a RadGrid

4 Answers 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 17 Apr 2012, 06:25 PM
Hi everyone,
i want to add an ImageColumn at the end of a RadGrid which is using a DataTable as DataSource.
DataTable dtTable = objTest.ReturnsDataTable();
RadGrid2.DataSource = dtTable;
RadGrid2.DataBind();
GridImageColumn gridImageCol = new GridImageColumn();
gridImageCol.ImageUrl = "/images/stift_icon.gif";
GridImageButton gridButton = new GridImageButton(gridImageCol);
gridButton.OnClientClick = "testAlert";
 
 
RadGrid2.DataBind();
RadGrid2.Columns.Add(gridImageCol);
//RadGrid2.Rebind();
RadGrid2.DataBind();
Because of the Source (i guess) i need to call the Binding-Metod, so that the row can be added (at the end).
If i just now add the Column without calling DataBind() or Rebind() again, the ImageColumn will not be shown. And by using those methods, the ImageColumn is the first Column.

Is there a way to solve this?
Is it maybe possible to switch the position of the columns? (I could use this in another case).

Thanks in advance,
Marcel

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 18 Apr 2012, 06:19 AM
Hello Marcel,

One suggestion is you can swap columns dynamically. Here is the sample code.
C#:
protected void grid1_PreRender(object sender, EventArgs e)
{  
 grid1.MasterTableView.SwapColumns("UniqueName1", "UniqueName2");
}

Thanks,
Shinu.
0
Marcel
Top achievements
Rank 1
answered on 18 Apr 2012, 09:48 AM
Hello Shinu,
thank you for your response. The SwapColumn-method will sure be helfpful for some of my other concerns but i still have the problem of adding the the ImageColumn to the Gridview. Here is the Code i use:

DefaultCS.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.IO;
using System.Data.Odbc;
 
namespace TelerikNestedGrid
{
    public partial class DefaultCS : System.Web.UI.Page
    {       
        private void DefineGridStructure()
        {
 
            RadGrid RadGrid1 = new RadGrid();
            RadGrid1.ID = "RadGrid1";
             
            //Assignt the DataTable as Source
            RadGrid1.DataSource = GetTableFromCSV(@"D:\RadGridImport.csv"); ;
 
            //If only Bind the Data here, the ImageColumn will not appear.
            RadGrid1.DataBind();
 
            //Create and Add the new Column
            GridImageColumn col = new GridImageColumn();
            col.ImageUrl= "/images/stift_icon.gif";
            GridImageButton btn = new GridImageButton(col);
            RadGrid1.Columns.Add(col);
 
            //If i first Bind and then Rebind() here, the ImageColumn is at the Beginning.
            //RadGrid1.Rebind();
 
            this.PlaceHolder1.Controls.Add(RadGrid1);
        }
 
 
        protected void Page_Init(object source, System.EventArgs e)
        {
            DefineGridStructure();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        private DataTable GetTableFromCSV(string path)
        {
            if (!File.Exists(path))
                throw new FileNotFoundException();
 
            FileInfo fileInfo = new FileInfo(path);
            DataTable dataTable = new DataTable();
            string connectionString = String.Format("Driver={{Microsoft Text Driver (*.txt; *.csv)}};Dbq={0};", fileInfo.DirectoryName);
            OdbcConnection connection = new OdbcConnection(connectionString);
            OdbcDataAdapter da = new OdbcDataAdapter(String.Format("select * from [{0}]", fileInfo.Name), connection);
            da.Fill(dataTable);
            return dataTable;
        }
    }
}

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DefaultCS.aspx.cs" Inherits="TelerikNestedGrid.DefaultCS" %>
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/tr/xhtml11/DTD/xhtml11.dtd">
<head id="Head1" runat="server">
    <!-- custom head section -->
    <!-- end of custom head section -->
</head>
<body class="BODY">
    <form runat="server" id="mainForm" method="post">
    <!-- content start -->
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
    <br />
    <div style="width: 730px;">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
        <br />
    </div>
 
    <!-- content end -->
    </form>
</body>
</html>
0
Marcel
Top achievements
Rank 1
answered on 19 Apr 2012, 07:18 AM
Is there anybody who can solve this problem?
0
Maria Ilieva
Telerik team
answered on 23 Apr 2012, 08:11 AM
Hi Marcel,

I would suggest you to review the following hep topic which elaborates on similar scenario:
http://www.telerik.com/help/aspnet-ajax/grid-changing-structure-dynamically.html

Greetings,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Marcel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Marcel
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or