简体   繁体   English

连续选择一个单元的Datagrid问题

[英]Datagrid Issue With Selecting One Cell Out Of A Row

I am going to try to make it very short and sweet. 我将尝试使其变得简短而甜美。 I have seen how to do this with a datagridview but I can't seem to find it with a normal datagrid. 我已经看到了如何使用datagridview做到这一点,但是我似乎无法使用普通的datagrid找到它。 I have a SelectedCellsChanged event that fires off that I want to get a field in a column at the selected row. 我有一个SelectedCellsChanged事件,该事件触发后,我想在所选行的列中获取一个字段。 So when they select an enite row I want to pull the truckercode field so I can do a SQL query on the truckercode cell of that row. 因此,当他们选择一个enite行时,我想拉出truckercode字段,这样我就可以对该行的truckercode单元执行SQL查询。 I am not doing a Binding for my datagrid. 我没有为我的数据网格进行绑定。 My code for how I fill my data grid is 我如何填充数据网格的代码是

using (SqlConnection connection = new SqlConnection("server=Server; database=db; user id=user; password=user"))
{
      connection.Open();
      using (SqlCommand command = new SqlCommand("SELECT * FROM SOMETABLE JOIN OTHERTABLE ON SOMETABLE.TRUCKERCODE = OTHERTABLE.TRUCKERCODE WHERE SOMETABLE.ACTIVE = 1 AND OTHERTABLE.ACTIVE = 1", connection))
      {
             SqlDataAdapter reader = new SqlDataAdapter(command);
             DataSet dataSet = new DataSet();
             reader.Fill(dataSet);
             FirstGrid.ItemsSource = dataSet.Tables[0].DefaultView;
      }
      connection.Close();
      connection.Dispose();
}

I think based on DataGrid Get Selected I think my code should look similar to 我认为基于DataGrid Get Selected,我认为我的代码应类似于

private void FirstGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        DataGrid grid = sender as DataGrid;
        DataRowView row = grid.SelectedItems as DataRowView;
        MessageBox.Show("" + grid.SelectedItems.Count);
        if (grid.SelectedItems.Count > 0)
        {
            MessageBox.Show(row["TruckerCode"].ToString());
        }
    }

But I get a Argument out of range exception. 但是我得到了一个超出范围异常的参数。

Another helpful post is Looping Through A DataView 另一个有用的文章是“ 循环遍历DataView”

I Solved my own problem. 我解决了自己的问题。 I hope anyone that reads this in the future that wants to get a value from selected columns from an entire selected row would use this. 我希望将来阅读此内容并希望从整个选定行中的选定列中获取值的任何人都可以使用它。 This example would print each cell for however many rows they have selected. 本示例将为每个单元格选择多少行打印每个单元格。

var grid = FirstGrid.SelectedItems;
foreach(DataRowView row in grid)
{
    if (grid.Count == 1)
    {
       MessageBox.Show(row["NameOfTheColumn"].ToString());
    }
}

If you add the grid.Count == 1 it prevents multiple rows from firing off your code. 如果添加grid.Count == 1,则可以防止触发多行代码。 So you only have 1 row in the DataRowView otherwise you may have multiple DataRowViews. 因此,DataRowView中只有1行,否则可能会有多个DataRowViews。

You can define a CellClick handler in your dataGrid 您可以在dataGrid中定义CellClick处理程序

<DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Margin="235,12,0,0" VerticalAlignment="Top" Height="218" Width="501" 
              AutoGenerateColumns="True" ItemsSource="{Binding}">
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <EventSetter Event="GotFocus" Handler="CellClick"/>
            </Style>
        </DataGrid.CellStyle>
</DataGrid>

Following method will then be triggered in your background class, once the user clicks on a cell in the dataGrid. 用户单击dataGrid中的单元格后,将在您的后台类中触发以下方法。 'index' is the value the selected cell includes: “索引”是所选单元格包括的值:

void CellClick(object sender, RoutedEventArgs e)
    {
        DataGridCell cell = sender as DataGridCell;
        string index = cell.Column.DisplayIndex.ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM