2013-11-05 79 views
0

我創建了一個包含數據網格的WPF vb.net表單。在datagrid內部,我有兩個組合框和兩個文本框,一個用於文章的組合框文本框,另一個用於服務。我應該首先綁定到List(Of article)類型的屬性,其次是List(Of service)類型,其中article是包含兩個公共屬性(articleId和articleName)的公共類,而service是包含兩個公共屬性(serviseId和serviceName )。文本框應顯示文章和服務名稱,組合框應顯示ID。當組合選擇被改變時,文本框文本也應該改變它的值。databinding datagrid的組合框和文本框到靜態資源列表

列表(文章)和列表(服務)應該從數據庫填充。

我怎麼能這樣做,我知道解決方案是在我身邊的某個地方,但根本無法抓住它。有兩個主要問題,數據庫綁定控件和推薦列表。

如果我需要發佈一部分代碼,我會這樣做,只是讓我知道。

請幫我解決這種情況,

謝謝。

+0

請發表您的代碼 – lena

+0

嗨,lena,請讓我知道如何發佈代碼。 – dragy

+0

不知道我是否完全明白你想做什麼。爲什麼你需要一個DataGrid?你希望行數等於表格行中的行數? – lena

回答

0

第一所有 - 對不起,我不是很熟悉VB.Net,所以我的代碼是用C#編寫的。由於我不瞭解你實際需要什麼,我只能給你提供一個不真正優雅的解決方案。

對於將您的ComboBox綁定到數據庫中的項目,您需要使用StaticResource,正如我所看到的那樣,但也許您將其定義爲錯誤,這就是爲什麼它不適用於您。爲了得到它的工作,你需要有一類這樣的:

public class artiklsList : List<artikl> 
{ 
    public artiklsList() 
    { 
     this.Add(new artikl(1, "first")); //this is dummy items, you need to do a database stuff here 
     this.Add(new artikl(2, "second")); 
     this.Add(new artikl(3, "third")); 
    } 
} 

而一個XAML這樣的:

<Window.Resources> 
    <my:artiklsList x:Key="source"></my:artiklsList> 
</Window.Resources> 

您還需要更新使細胞與文本當ComboBox選擇改變。這不是一項簡單的任務,因爲使用BindingElementName進行控制的最簡單方法不適用於DataGrid。我所做的是有點哈克反正...

因此,XAML是不是很複雜:

<DataGrid Name="dgrStavke" AutoGenerateColumns="False" Height="160" Width="600" HorizontalAlignment="Left" Margin="5" Grid.Row="7" Grid.ColumnSpan="4" > 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Artikl ID"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox SelectedIndex="{Binding selectedIndexID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="cmbArtikli" Width="120" DisplayMemberPath="artiklId" ItemsSource="{StaticResource source}"> 
         </ComboBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTextColumn Header="Naziv artikla" Binding="{Binding nazivArtikla}"/>   
     </DataGrid.Columns> 
    </DataGrid> 

SelectedIndex結合和可怕的代碼,這樣做背後的伎倆與更新文本單元。爲了使綁定工作正常,articl類必須執行INotifyPropertyChanged接口。

public class artikl: INotifyPropertyChanged 
{ 
    public artikl(int artid, string nazivart) 
    { 
     artiklId = artid; 
     nazivArtikla = nazivart; 
    } 

    public int artiklId{get;set;} 

    private string _nazv; 
    public string nazivArtikla 
    { 
     get { return _nazv; } 
     set { _nazv = value; NotifyPropertyChanged("nazivArtikla"); } 
    } 

    //Here I think you may have questions 
    private int _index; 
    public int selectedIndexID 
    { 
     get 
     { 
      //To get a SelectedIndex for ComboBox in current row we look in 
      //listArtikli defined in a MainWindow for a articli item with a current 
      //item's Id and take the index of this item 
      artikl art = MainWindow.listArtikli.Find(el => el.artiklId == this.artiklId); 
      return MainWindow.listArtikli.IndexOf(art); 
     } 
     set 
     { 
      //This property is binded with SelectedIndex property of ComboBox. 
      //When selected index changed, we look in listArtikli and take 
      //here properties of item with this index. 
      //This will change values of item binded to the current grid row 
      _index = value; 
      this.nazivArtikla = MainWindow.listArtikli[value].nazivArtikla; 
      this.artiklId = MainWindow.listArtikli[value].artiklId; 
      NotifyPropertyChanged("selectedIndexID"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

和窗口隱藏代碼:

public partial class MainWindow : Window 
{ 
    public static artiklsList listArtikli = new artiklsList(); 
    public static artiklsList gridsource = new artiklsList(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     dgrStavke.ItemsSource = gridsource; 
    } 
} 

你需要gridsource除了listArtikli與價值觀,以填補你DataGrid。也因爲所有此代碼在selectedIndexID如果您只使用listArtikli其值損壞。因此listArtikli包含articli項目,它們按照檢索順序排序。並且gridsource包含在DataGrid中呈現的對artiklId - nazivArtikla。 希望它能幫助你一點點。

0

感謝您的關注。如果您需要更多說明,請告訴我。這是一對組合框,文本框的代碼,我會很容易申請到其他的一個: XAML ...

<DataGrid Name="dgrStavke" AutoGenerateColumns="False" Height="160" Width="600" HorizontalAlignment="Left" Margin="5" Grid.Row="7" Grid.ColumnSpan="4"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Header="Artikl ID"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox Name="cmbArtikli" Width="120" ItemsSource="{Binding Source={StaticResource artcls}, Path=listArtikli}" DisplayMemberPath="artiklId"></ComboBox> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate>      
       </DataGridTemplateColumn> 
       <DataGridTextColumn Header="Naziv artikla" Binding="{Binding nazivArtikla}"></DataGridTextColumn> 
      </DataGrid.Columns> 
     </DataGrid> 

...

CODE

Imports System.Data 
Imports System.Data.SqlClient 
Imports System.Text 
Namespace MW 

    Public Class artikl 

     Sub New(artid As Integer, nazivart As String) 
      ' TODO: Complete member initialization 
      artiklId = artid 
      nazivArtikla = nazivart 
     End Sub 

     Public Property artiklId() As Integer 
     Public Property nazivArtikla() As String 
    End Class 

    Public Class frmDodavanjePaketa 
    Public Property listArtikli() As New List(Of artikl) 

    Private Sub popuniComboArtikli() 
      Dim sqlConn As SqlConnection = New SqlConnection(moduleGlobal.connString) 
      sqlConn.Open() 
      Dim strSql As New StringBuilder 
      strSql.Append("select a.artiklId, a.nazivArtikla ") 
      strSql.Append(" from artikli a ") 
      strSql.Append(" where isnull(a.aktivan, 0) = 1") 

      Dim sqlCom As SqlCommand = New SqlCommand(strSql.ToString, sqlConn) 
      Dim sqlDs As DataSet = New DataSet 
      Dim sqlDa As SqlDataAdapter = New SqlDataAdapter 
      sqlDa.SelectCommand = sqlCom 
      sqlDa.Fill(sqlDs) 

      For Each row As DataRow In sqlDs.Tables(0).Rows 
       Me.listArtikli.Add(New artikl(row.ItemArray(0).ToString, row.ItemArray(1).ToString)) 
      Next 

     End Sub 
     End Class 
     End Namespace