Search code examples
c#telerikradlistview

Centering specific column in RadListView (Telerik, Winforms)


I'm using a (Telerik) RadListView (Telerik.WinControls.UI.RadListView()) in my project. The listview has a few columns:

public MainForm()
{
    Telerik.WinControls.UI.ListViewDetailColumn newColumn;
    InitializeComponent();

    newColumn = new ListViewDetailColumn("ID", "ID");
    newColumn.Width = 250;
    newColumn.Visible = false;
    this.MyListView.Columns.Add(newColumn);

    newColumn = new ListViewDetailColumn("Name", "Name");
    newColumn.Width = 250;
    newColumn.Visible = true;
    this.MyListView.Columns.Add(newColumn);

    newColumn = new ListViewDetailColumn("Description", "Description");
    newColumn.Width = 250;
    newColumn.Visible = true;
    this.MyListView.Columns.Add(newColumn);

    newColumn = new ListViewDetailColumn("Costs", "Costs");
    newColumn.Width = 250;
    newColumn.Visible = true;
    this.MyListView.Columns.Add(newColumn);

I'm populating the listview manually by adding ListViewDataItems to it:

foreach (Data.Tablename listEntry in ListOfTableEntries)
{
    ListViewDataItem newRow = new ListViewDataItem(listEntry.Id, new string[]{ listEntry.Name, listEntry.Description, listEntry.Costs});
    this.MyListView.Items.Add(newRow);
}

So far so good. What I did not find out though is how I can format specific columns that are making up the added row. For example: Right-align the costs. How can I do that (without also right-aligning all other columns)?

Thanks


Solution

  • Try something along these lines:

       private void radListView1_CellFormatting(object sender, ListViewCellFormattingEventArgs e)
       {
        if ((e.CellElement).Data.HeaderText == "ID")
        {
            if ((e.CellElement is DetailListViewDataCellElement))
            {
                e.CellElement.TextAlignment = ContentAlignment.TopRight;
            }
        }
        if ((e.CellElement).Data.HeaderText == "Name")
        {
            if ((e.CellElement is DetailListViewDataCellElement))
            {
                e.CellElement.TextAlignment = ContentAlignment.MiddleCenter;
            }
    
        //end so on
        }
    }