2013-08-30 33 views
2

我有一個小型數據庫(sql compact)的數據網格 表包含500多行,每個塊包含不同的節號;在wpf或winform中使用datagrid(或datagridview)時遇到問題

的Xamle代碼:

<Window x:Class="WpfApplication9.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="440" Width="690" Loaded="Window_Loaded"> 
<Grid> 
    <Button Content="To TextBox" Height="29" HorizontalAlignment="Left" Margin="561,272,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
    <DataGrid AutoGenerateColumns="True" Height="234" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="644" /> 
    <CheckBox Content="Section Rate" Height="16" HorizontalAlignment="Left" Margin="126,313,0,0" Name="checkBox1" VerticalAlignment="Top" /> 
    <CheckBox Content="Section Result" Height="16" HorizontalAlignment="Left" Margin="28,313,0,0" Name="checkBox2" VerticalAlignment="Top" /> 
    <CheckBox Content="Discipline" Height="16" HorizontalAlignment="Left" Margin="28,272,0,0" Name="checkBox3" VerticalAlignment="Top" /> 
    <TextBox Height="54" HorizontalAlignment="Left" Margin="240,324,0,0" Name="textBox1" VerticalAlignment="Top" Width="396" /> 
    <CheckBox Content="Total Biology" Height="16" HorizontalAlignment="Left" Margin="113,272,0,0" Name="checkBox4" VerticalAlignment="Top" /> 
    <CheckBox Content="Math" Height="16" HorizontalAlignment="Left" Margin="28,351,0,0" Name="checkBox5" VerticalAlignment="Top" /> 
    <CheckBox Content="Physics" Height="16" HorizontalAlignment="Left" Margin="113,351,0,0" Name="checkBox6" VerticalAlignment="Top" /> 
</Grid> 

守則behinde:

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     DataRowView _data = dataGrid1.CurrentCell.Item as DataRowView; 
     if (_data != null) 
     { 
      MessageBox.Show(_data.Row[0].ToString()); 
     } 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\MyDatabase1.sdf"); 
     con.Open(); 
     SqlCeDataAdapter da = new SqlCeDataAdapter("select * from StudentGroup", con); 
     DataTable dt = new DataTable(); 
     DataSet ds = new DataSet(); 
     da.Fill(dt); 
     dataGrid1.ItemsSource = dt.DefaultView; 
     //dataGrid1.ItemsSource = ds.Tables[0].DefaultView; 
     con.Close(); 
    } 

我想要的數據TextBox1的基礎上選擇複選框輸出,格式如下: 線1 :組號,組名,學科(或生物學總數:生物學專欄中的行結果總和) 對於選定G中的每個片段編號roup編號: 下一行:(第速率或段的結果,數學或物理)==>基於選擇的複選框

例如:

線1:1,GPA,100(或137 =總生物學) 如果SectionResultcheckbox和mathchechbox既是檢查: 下一行會:80,80,90,70,54,31

我已經試過這樣:

DataRowView _data = dataGrid1.CurrentCell.Item as DataRowView; 
     if (_data != null) 
     { 
      MessageBox.Show(_data.Row[0].ToString()); 
     } 

,但它不工作。

我不知道如何處理這個問題? 感謝您的任何幫助。

+0

是什麼問題?什麼不工作?你有什麼麻煩?如果不知道病情,就不能給予藥物:) – Ehsan

+0

@沒有人:我已經解釋了:我想根據選中的複選框將數據導出到textbox1,格式如下:第1行:組號,組名,學科(或總數生物學:生物學專欄中的行結果總和)以及選定組編號中的每個分節編號:下一行:(分節速率或分節結果,數學或物理)==>基於選定的複選框 –

+0

@SmartMan當「 Discipline' checkbox is checked?...應該輸出什麼? – Rohit

回答

1

如果Grid SelectionUnit設置爲「FullRow」,則使用下面的代碼。

((DataRowView)dgGroup.SelectedItem).Row -> This will give you current data row 

((DataRowView)dgGroup.SelectedItem).Row.ItemArray[0] -> This will give first column value 

((DataRowView)dgGroup.SelectedItem).Row.ItemArray[1] -> This will give second column value 

如果Grid SelectionUnit設置爲「Cell」,則使用下面的代碼。

(DataRowView)dgGroup.SelectedCells[0].Item -> This will give you current data row 

((DataRowView)dgGroup.SelectedCells[0].Item).Row.ItemArray[0] -> This will give first column value 

根據您的評論添加更多的代碼來遍歷節。

  // Find index of selected row which would be group/block row. Add 1 to find first section row in that block/group. 
     var index = dgGroup.Items.IndexOf(dgGroup.SelectedItem) + 1; 

     // Starting from the index found above loop through section rows untill you find blank row which can be identified by checking if "Group Name" does not have any value. 
     for (int i = index; i < dgGroup.Items.Count; i++) 
     { 
      if (((DataRowView)dgGroup.Items[i]).Row.ItemArray[1].ToString().Trim() == string.Empty) 
      { 
       return; 
      } 
      else 
      { 
       // Add data to textbox. 
      } 
     } 

爲了提高使用體驗,您還可以考慮放一些限制,以便用戶只能選擇組/塊而不選擇部分。如果那件衣服你需要。

+0

謝謝,但我測試過你的anwser:textBox1.Text =((DataRowView)dgGroup.SelectedItem).Row.ToString給系統.Data.DataRowView但((DataRowView)dgGroup.SelectedItem).Row.ItemArray [0]起作用。我如何循環行直到空行?如你所見,每個塊由一個emty行分隔。謝謝。 –

+0

@ RonakThakkar:嗨,你能介意幫助我嗎?非常感謝。 http://stackoverflow.com/questions/19110613/how-to-get-different-index-of-2-row-in-datagrid-in-wpf-c –

0

感謝RonakThakkar,我已投票給他有用的答案。

由於電網SelectionUnit被設置爲「FullRow」所以回答我quesion的第一部分是:

private void button1_Click(object sender, RoutedEventArgs e) 
{ 

    if (((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[0].ToString() != string.Empty) 
    { 
     textBox1.Text=((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[0].ToString()+","+((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[1].ToString()+","+((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[7].ToString(); 
    } 
} 

,結果在文本框將是:1,GPA,100

因此保持部分我如何循環行直到分隔每塊的空行?

更多解釋:例如,如果用戶選擇第一行,並sectionresultcheckbox和mathcheckbox檢查,按鈕點擊,我們應該在文本框中: 1,GPA,100 +下一行+ 80 , 80, 90 ,70 ,54 ,31 感謝您的幫助。