Datagrid Row and Column in Silverlight


Introduction

This article describe the datagrid events to get row content, changing particular column color, hiding particular column at runtime in silverlight.

We can get the row content of datagrid at runtime with the help of datagrid events. We can also change the color of particular column in silverlight at runtime.

We can hide the particular column of datagrid at runtime.

Step 1: Get the row content at runtime using LoadingRow Event of datagrid

First datagrid looks like as following

Datagrid Row and Column in Silverlight

Get the loadingrow event of silverlight datagrid as follow.Here we change the row content color using loadingrow event at runtime.

EventHandler<DataGridRowEventArgs>(Customerdatagrid_LoadingRow);

void Customerdatagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
            // CustomerDetail is a table at service side
            ServiceReference1.CustomerDetail clist = e.Row.DataContext as ServiceReference1.CustomerDetail;
            FrameworkElement el;
            el = this.Customerdatagrid.Columns[1].GetCellContent(e.Row);
            DataGridCell changeCell = GetParent(el, typeof(DataGridCell)) as DataGridCell;
            SolidColorBrush brush = new SolidColorBrush(Colors.Black);
            if (changeCell != null)
            {
                if (clist.value > 0)
                {
                    brush = new SolidColorBrush(Colors.Green);
                }
                else if (clist.value < 0)
                {
                    brush = new SolidColorBrush(Colors.Red);
                }
                changeCell.Foreground = brush;
            }
          
    }

Datagrid looks like as following

Datagrid Row and Column in Silverlight

Step 2: Change the color of particular column in datagird using LayoutUpdated event

//DatagridCell Style
<UserControl.Resources>
        <Style TargetType="sdk:DataGridCell" x:Key="DataGridCellStyle">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Foreground" Value="#FF0059C0"/>
            <Setter Property="FontStyle" Value="Normal"/>
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
        </Style>
    </UserControl.Resources>
 Customerdatagrid.LayoutUpdated += new EventHandler(Customerdatagrid_LayoutUpdated);

       Boolean headerflag = false;
        void Customerdatagrid_LayoutUpdated(object sender, EventArgs e)
        {
            if (headerflag)
            {

                DataGridColumn col = Customerdatagrid.Columns[2];
                col.CellStyle = (Style)Resources["DataGridCellStyle"];
                headerflag = false;
            }
        }

Datagrid looks like as following

Datagrid Row and Column in Silverlight

Step 3: Hide the particular column of datagrid using AutoGeneratingColumn Event

Customerdatagrid.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(Customerdatagrid_AutoGeneratingColumn);
void
Customerdatagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)

{
     headerflag = true;
     DataGridTextColumn tcolumn = (DataGridTextColumn)e.Column;
     string headercolumn = e.Column.Header.ToString();
     if (headercolumn == "Name")
     {
         tcolumn.Visibility = Visibility.Collapsed;
     }
 }


Datagrid looks like as following

Datagrid Row and Column in Silverlight

Summary : We can hide the datagrid column at runtime also we can change the color of datagrid column using datagird events. Using loadingrow events we can get the row content at runtime.
 


Similar Articles