Home > DeveloperSection > Forums > Selecting a row in DataGrid
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    February-02-2014 11:57 PM

 C# C# 
Ratings:


 1 Reply(s)
 716  View(s)
Rate this:

I defined an initial Table with three rows. If a user select a row and clicks a "Start new Table" button, it will open a new tabItem with a new Table. the Problem is that i dont know how can i select an entire row in my DataGrid.

C# Code:

//this my initial Table

private ObservableCollection<TableDataRowStringItem> tableobject = new ObservableCollection<TableDataRowStringItem>();

List<TableDataRowStringItem> rowstringList = new List<TableDataRowStringItem>();

TableDataRowStringItem item = new TableDataRowStringItem();

item.RowNumber = 1; item.saveFlag = true; item.ObjectType = "E"; item.Name = "E"; item.PredecessorRowNumber = "0";

rowstringList.Add(item);

item = new TableDataRowStringItem();

item.RowNumber = 2; item.ObjectType = "Function"; item.Name = "Function";    item.PredecessorRowNumber = "1";

rowstringList.Add(item);

item = new TableDataRowStringItem();

item.RowNumber = 3; item.ObjectType = "E"; item.Name = "E"; item.PredecessorRowNumber = "2";

rowstringList.Add(item);

rowstringListEPK = rowstringList;

for (int i = 0; i < rowstringList.Count; i++)

{

    tableobject.Add(rowstringList[i]);

}

DataGrid1.ItemsSource = tableobject;

//Button Code

foreach (TableDataRowStringItem item in rowstringListEPK)

{

    if (item.ObjectType == "Function" **(&& Hier i schould write if row.Isselected)**)

    {

       rowStringItem.Name = item.Name;

       tabControl.Items.Add(tabItem);

       tabItem.Focus();

       tabItem.IsSelected = true;

       tabItem.Header = rowStringItem.Name;

       TableTab.Visibility = Visibility.Visible

    }

    else do nothing

}

//XAML Code

 <DataGrid.RowStyle>

    <Style TargetType="DataGridRow">

        <Style.Triggers>

            <Trigger Property="IsSelected" Value="True">

               <Setter Property="BorderBrush" Value="Blue" />

               <Setter Property="BorderThickness" Value="1" />

               <Setter Property="AllowDrop" Value="True" />

            </Trigger>

        </Style.Triggers>

    </Style>

 </DataGrid.RowStyle>



Pravesh Singh

Total Post:411

Points:2881
Posted on    February-03-2014 12:54 AM

Hi Ankita,

This may help:

<DataGrid SelectionMode="Single" SelectionUnit="FullRow" ...

or

DataGrid dataGrid = new DataGrid();

dataGrid.SelectionUnit = DataGridSelectionUnit.FullRow;

dataGrid.SelectionMode = DataGridSelectionMode.Single;


Don't want to miss updates? Please click the below button!

Follow MindStick