none
Get Cell Value of DataGridComboBox Column for WPF DataGrid RRS feed

  • Question

  • hello Friends

    I am using WPF Datagrid (WPFToolKit Control) in my appliction

    I Have a DataGridComboBox Column which is filled with some records. 

    <WpfToolkit:DataGridComboBoxColumn 
    ScrollViewer.CanContentScroll="True" 
    CellStyle="{StaticResource LeftAlignedCellStyle}" 
    Header="Item Code" x:Name="CmbItems" 
    Width="250" 
    SelectedValueBinding="{Binding ITEMCODE}" 
    DisplayMemberPath="ITEMDESC" 
    SelectedValuePath="ITEMCODE"  >
    </WpfToolkit:DataGridComboBoxColumn>
    
    Now how to get selectedText of that combobox for current row. my current focus on other cell 

    Please Help Me

    regards
    Vipul

    Friday, March 12, 2010 9:46 AM

Answers

  • Hi Vipul,

    > Now how to get selectedText of that combobox for current row

    What do you mean by "selectedText" in the above sentence? If you mean "SelectedValue" of the editing ComboBox control, you can get that value in the underlying data source since the DataGrid is data bound.

    If you want to get the editing ComboBox control, you can handle the PreparingCellForEdit event of the DataGrid and get the editing ComboBox control through the e.EditingElement parameter.

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Friday, March 19, 2010 3:12 AM
    Thursday, March 18, 2010 9:10 AM
  • Hi Vipul,

    Thank you for your reply! I understand your question now.

    You can call the DataGridColumn.GetCellContent(DataRow) method to get the FrameworkElement hosted in the cell under that DataGrid column. For DataGridComboBoxColumn, the FrameworkElement hosted in its cells is TextBlockComboBox, which inherits from ComboBox. Because the TextBlockComboBox isn't exposed to be used, we can cast the FrameworkElement to ComboBox and get the display text in it through its Text property.

    The following is a sample, which gets the text in the combobox cell of the selected row. It assumes the first column is a DataGridComboBoxCoumn.

     private void Button_Click(object sender, RoutedEventArgs e)
     {
                DataGridRow row = this.dg.ItemContainerGenerator.ContainerFromIndex(this.dg.SelectedIndex) as DataGridRow;
                ComboBox ele = this.dg.Columns[0].GetCellContent(row) as ComboBox;
                Console.WriteLine(ele.Text);
     }

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, March 23, 2010 5:10 AM
    Friday, March 19, 2010 11:03 AM

All replies

  • Hi Vipul,
          Can you post more complete code(both xaml and code behind) to me?I will see how to help you based on that.

    I am looking forward to you reply.
    Tuesday, March 16, 2010 6:45 AM
  • Hi Vipul,

    > Now how to get selectedText of that combobox for current row

    What do you mean by "selectedText" in the above sentence? If you mean "SelectedValue" of the editing ComboBox control, you can get that value in the underlying data source since the DataGrid is data bound.

    If you want to get the editing ComboBox control, you can handle the PreparingCellForEdit event of the DataGrid and get the editing ComboBox control through the e.EditingElement parameter.

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Friday, March 19, 2010 3:12 AM
    Thursday, March 18, 2010 9:10 AM
  • Hi Linda Liu

    By selectedText i mean Displaymember column, I have 2 columns ItemCode and ItemName in datatable

    i bound ItemCode column with SelectedValuepath and ItemName with Displaymemberpath. Now suppose there are 3 items in combo

    ItemCode ItemName

    1              Item1

    2              Item2

    3              Item3

    now when i open my combobox i will be able to see only ItemName as i have bounded to Displaymemberpath. suppose i selected Item2 and click on other cell in same row. now while changing value in that other cell i want what item i have selected in Item column (i.e. i want Item2 as name). i want its itemname and not itemcode

    hope this will make u understand my problem properly

    Regards

    Vipul

    Friday, March 19, 2010 6:04 AM
  • Hi Bruce-YU here is my sample code
    
    <Page x:Class="PO"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
        xmlns:y="clr-namespace:Infoworld"
        Title="PO" Width="824">
    <Grid Width="824" Height="352" Name="Grid1">
    <TabControl Margin="10,12,12,12" Name="TabPO" Background="LightGray" MaxHeight="500" VerticalAlignment="Top">
     <TabItem FontFamily="Verdana" Header="Detail" Name="TBDetail" Width="200" >
                    <Grid Height="241">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="448*" />
                            <ColumnDefinition Width="343*" />
                        </Grid.ColumnDefinitions>
                        <WpfToolkit:DataGrid Name="GV1" AutoGenerateColumns="False" xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit" Grid.ColumnSpan="2" Margin="0,0,6,6" RowHeight="25" ColumnHeaderHeight="25">
                            <WpfToolkit:DataGrid.Columns>
                               
                                <WpfToolkit:DataGridComboBoxColumn Header="Item Code" x:Name="CmbItems" Width="250" SelectedValueBinding="{Binding ITEMCODE}"  DisplayMemberPath="ITEMDESC" SelectedValuePath="ITEMCODE"  >                                
                                </WpfToolkit:DataGridComboBoxColumn>
                                                            <WpfToolkit:DataGridTextColumn Header="Qty." Binding="{Binding Path=QTY}" Width="100" >
                                    <WpfToolkit:DataGridTextColumn.EditingElementStyle>
                                        <Style TargetType="{x:Type TextBox}">
                                            <EventSetter Event="LostFocus" Handler="Qty_LostFocus" />
                                        </Style>
                                    </WpfToolkit:DataGridTextColumn.EditingElementStyle>
                                </WpfToolkit:DataGridTextColumn>
                                <WpfToolkit:DataGridTextColumn Header="Rate" Binding="{Binding Path=RATE}" Width="100">
                                    <WpfToolkit:DataGridTextColumn.EditingElementStyle>
                                        <Style TargetType="{x:Type TextBox}">
                                            <EventSetter Event="LostFocus" Handler="Rate_LostFocus" />
                                        </Style>
                                    </WpfToolkit:DataGridTextColumn.EditingElementStyle>
                                </WpfToolkit:DataGridTextColumn>
                                <WpfToolkit:DataGridTextColumn Header="Amount" Width="150" Binding="{Binding Path=AMOUNT}" IsReadOnly="True" />
                            </WpfToolkit:DataGrid.Columns>
                        </WpfToolkit:DataGrid>
                    </Grid>
    
                </TabItem>
    </TabControl>
            
        </Grid>
    </Page>
    
    Code Behind
    
    
    Private Sub Grid1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Grid1.Loaded
            Conn = New Connection()
            Conn.GetConnection()
            ''fill Item combo
            CmbQuery = " SELECT ITEM1.ITEMCODE, ITEM1.ITEMDESC FROM ITEM1"
            DtItems = New DataTable("Items")
              
            'Conn.DtInit(DtItems, CmbQuery) function will execute query and 
            ' fill the datatable with its result. It return true if filled 
            'successfully else false
    
            If (Conn.DtInit(DtItems, CmbQuery)) Then
                Dim view1 As DataView
                Dim Coll1 As BindingListCollectionView
                view1 = New DataView(DtItems)
                Coll1 = New BindingListCollectionView(view1)
                CmbItems.ItemsSource = Coll1
            End If
    End Sub
    
    now this is the event that is called when rate is changed and 
    i want selected  Item Name in this event
    
    
    Private Sub Rate_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
    
    End Sub
    Regards Vipul

    Friday, March 19, 2010 6:18 AM
  • Hi Vipul,

    Thank you for your reply! I understand your question now.

    You can call the DataGridColumn.GetCellContent(DataRow) method to get the FrameworkElement hosted in the cell under that DataGrid column. For DataGridComboBoxColumn, the FrameworkElement hosted in its cells is TextBlockComboBox, which inherits from ComboBox. Because the TextBlockComboBox isn't exposed to be used, we can cast the FrameworkElement to ComboBox and get the display text in it through its Text property.

    The following is a sample, which gets the text in the combobox cell of the selected row. It assumes the first column is a DataGridComboBoxCoumn.

     private void Button_Click(object sender, RoutedEventArgs e)
     {
                DataGridRow row = this.dg.ItemContainerGenerator.ContainerFromIndex(this.dg.SelectedIndex) as DataGridRow;
                ComboBox ele = this.dg.Columns[0].GetCellContent(row) as ComboBox;
                Console.WriteLine(ele.Text);
     }

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, March 23, 2010 5:10 AM
    Friday, March 19, 2010 11:03 AM
  • Hi Vipul,

    Thank you for your reply! I understand your question now.

    You can call the DataGridColumn.GetCellContent(DataRow) method to get the FrameworkElement hosted in the cell under that DataGrid column. For DataGridComboBoxColumn, the FrameworkElement hosted in its cells is TextBlockComboBox, which inherits qt from ComboBox. Because the TextBlockComboBox isn't exposed to be used, we can cast the FrameworkElement to ComboBox and get the display text in it through its Text property.

    The following is a sample, which gets the text in the combobox cell of the selected row. It assumes the first column is a DataGridComboBoxCoumn.

     private void Button_Click(object sender, RoutedEventArgs e)
     {
                DataGridRow row = this.dg.ItemContainerGenerator.ContainerFromIndex(this.dg.SelectedIndex) as DataGridRow;
                ComboBox ele = this.dg.Columns[0].GetCellContent(row) as ComboBox;
                Console.WriteLine(ele.Text);
     }

    Hope this helps.
    If you hav any question, please feel free to let me know.

    Sincerely,
    Linda Liu

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    I learned a lot from what you said, Thanks a lot!
    Thursday, September 16, 2010 1:41 PM
  • This is fine, so I can get the text that we selected in the combobox, but how can I get the value (id)?
     DataGridRow row = this.dataGridMain.ItemContainerGenerator.ContainerFromIndex(this.dataGridMain.SelectedIndex) as DataGridRow;

     ComboBox value = this.dataGridMain.Columns[5].GetCellContent(row) as ComboBox;

     MessageBox.Show(value.Value);

    • Edited by eli230984 Thursday, July 13, 2017 8:38 PM
    Thursday, July 13, 2017 8:11 PM
  • You are not going to get much help with a question in an already answered post.  To get help post a new question and show some XAML or at least something to go on to help us help you.

    Lloyd Sheen

    Thursday, July 13, 2017 8:33 PM