Reputation:
Can i get value Hidden column from DataGrid?
<DataGridTextColumn Header="" Width="10" Binding="{Binding id}" Visibility="Hidden"/>
Using this code, i get exception.
Data.IdServ = ((TextBlock)DataGridService.Columns[1].GetCellContent(row)).Text;
if (dgUserEnroll.SelectedItem != null)
{
var data = (User)dgUserEnroll.SelectedItem;
var userID = data.UserId;
}
this is a not option, because i have tableadapter when receiveng data
Upvotes: 2
Views: 3204
Reputation: 1
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="delete" Width="40" Click="Delete_Click" Background="#FFD80000">
<materialDesign:PackIcon Kind="Delete" Width="25" Margin="-10,0,0,0" FontWeight="Bold"></materialDesign:PackIcon>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Edit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="edit" Width="40" Click="Edit_Click" Background="#FF673AB7">
<materialDesign:PackIcon Kind="Edit" Width="25" Margin="-10,0,0,0" FontWeight="Bold"></materialDesign:PackIcon>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTextColumn Width="300" Binding="{Binding [2]}" Header="Price"></DataGridTextColumn>-->
</DataGrid.Columns>
Upvotes: 0
Reputation: 71
First convert selected item of data grid view to ItemsSource
of data grid view:
dataGridUser.ItemsSource is View_Users ==>
dataGridUser.ItemsSource = database.Database.SqlQuery<View_Users>(
"select * from view_users where 1=1"+searchString()).ToList();
Then, to get value Hidden
or Visible
column from DataGrid
:
var id= ((View_Users)dataGridUser.SelectedItem).UserID;
Upvotes: 1
Reputation: 463
I came with a simpler solution, suppose you have bound a List to the Datagrid, YourClass has Id property the XAML would look like :
<DataGrid x:Name="ListeUrls" AutoGenerateColumns="False" Margin="1,0,-1,27" >
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Visibility="Hidden"></DataGridTextColumn>
<DataGridTextColumn Header="Vendor" Binding="{Binding Vendor}" Foreground="red" FontWeight="Bold" ></DataGridTextColumn>
<DataGridTextColumn Header="Url" Binding="{Binding url}" ></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
In the code behind :
Somewhere you have bound the ListeUrls: ListeUrls.ItemsSource = new List{ .... };
private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var dataGridCellTarget = (DataGridCell)sender;
var parent = VisualTreeHelper.GetParent(dataGridCellTarget);
.....
}
You use the VisualTreeHelper to get the parent of the cell on which you triggered the mouse doubleclick. The parent has the properties of YourClass thus the Id.
I think no need to set the width of the hidden DataGridColumn, set its Visibility to Hidden is enough.
Upvotes: 0
Reputation: 5666
You can use your code behind too. You just need to hide the column in a different way:
<DataGridTextColumn Header="" MaxWidth="0" Binding="{Binding id}" />
i.e. remove the Visibility
attribute and set MaxWidth
to zero.
Upvotes: 3
Reputation:
I found two ways
first
string ID = ((DataRowView)DataGridService.SelectedItem).Row["id"].ToString();
second
var data = (DataRowView)DataGridService.SelectedItem;
var userId = data.Row["id"];
Upvotes: 2
Reputation: 1510
You have a Binding with id field, so use it instead of accessing cell content.
Upvotes: 1