0

I have a table (WareTable in SQL Server).

I need to use a datagridview(=dgv) in my form.

In my dgv , 1st column is a ComboBox (WareName).

I want when choose a value in ComboBox , next column (sellPrice) will be filled automatically.

How i can ?

matzone
  • 5,703
  • 3
  • 17
  • 20
Majid
  • 101
  • 2
  • 11

1 Answers1

0

Would a multibinding help you? If you have a viewmodel for each row in your data grid view, selecting a value in column 1 should set a property on your viewmodel. You could setup a multibinding and a value converter that will bind to the property from column 1 and whatever other data you need and display the correct value in column 2.

<DataGridTextColumn 
  Header="Ticket Qty" 
  Width="Auto" 
  IsReadOnly="True" >
  <DataGridTextColumn.Binding>
    <MultiBinding Converter="{StaticResource unitConverter}">
      <Binding Path="Column1ViewModelProperty"/>
      <Binding Path="Column2ViewModelProperty" />
      <Binding Path="AdditionalViewModelProperty"/>
    </MultiBinding>
  </DataGridTextColumn.Binding>
</DataGridTextColumn>

http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.converter.aspx

Alternatively, you could have any changes to your Column1ViewModelProperty update your Column2ViewModelProperty in your actual view model class (in the setter for Column1ViewModelProperty raise an event or call a method that will update Column2ViewModelProperty).

<DataGridTextColumn Binding="{Binding Column1ViewModelProperty }"/>

//Code behind
public string Column1ViewModelProperty {
  get{
    return _column1ViewModelProperty;
  }
  set{
    _column1ViewModelProperty = value;
    Column2ViewMOdelProperty = calculatedValue;
    OnPropertyChanged("Column1ViewModelProperty");
  }
}

public string Column2ViewModelProperty {
  get{
    return _column2ViewModelProperty;
  }
  set{
    _column2ViewModelProperty = value;
    OnPropertyChanged("Column2ViewModelProperty");
  }
}
Darlene
  • 808
  • 5
  • 16
  • by tnx , what do i do by C# coding ? I don't know XML coding now . – Majid Jun 08 '13 at 18:02
  • What DataGridVeiw control are you using? Based on your description I assumed it was a WPF DataGridView. If you are I added some more c# code to my answer. – Darlene Jun 10 '13 at 15:02