如何从datagrid单元格中获取字符串值?

时间:2011-10-05 10:56:10

标签: c# wpf

这是我的第一篇文章。我是C#WPF的新手,所以我可能完全不在思考。我基本上试图创建一个像电子表格一样的数据网格,以便在用户输入总和时,例如。 10 * 10,在CellEditEnding上,它将在单元格中显示答案,如excel。

我有一个6x6网格绑定到一个空的ObservableCollection,当用户键入10 * 10我试图获取他们输入的字符串时,将其拆分,然后使用这些部分创建计算但是在分钟我无法从单元格中获取字符串。当我直接在代码中设置字符串时,计算有效。

真的很感激任何帮助。也许有一种更简单的方法可以做到这一点。

public partial class MainWindow : Window
{
    ObservableCollection<DataCell> spreadsheet = new ObservableCollection<DataCell>();

    public MainWindow()
    {
        //Communal Spreadsheet
        InitializeComponent();

        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));
        spreadsheet.Add(new DataCell("", "", "", "", "", ""));

        dataGrid2.ItemsSource = spreadsheet;

}

private void dataGrid2_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        // I want to get the current cell value using the below
        // string myString = dataGrid1.CurrentItem.ToString();

        // the calculation seems to work with this when i pass it to textbox1 and set the myString Variable in the code.   
        string myString = "10 * 10";

        string[] parts = myString.Split(' ');

        int part1 = int.Parse(parts[0]);
        //    string part2 = parts[1];
        int part3 = int.Parse(parts[2]);

        int answer = part1 * part3;
        string answer2 = answer.ToString();

        // dataGrid2.CurrentItem = answer2;

        // seems to work when i pass to the below textbox instead of back to the cell above 
        textBox1.Text = answer2;
    }
}

}

3 个答案:

答案 0 :(得分:12)

假设您正在编辑DataGridTextColumn单元格...

使用“{”这样的优势DataGridCellEditEndingEventArgs

 ((TextBox)e.EditingElement).Text

这将为您提供输入的文字。

答案 1 :(得分:0)

也许为了解决我需要弄清楚的解决方案的空白,我会分享。对于dataGrid,我需要单元格,列,行和值来做我想要的,但我只能获得完整的旧信息和隔离的新单元格值。只是让你知道ControlLimit是什么:

public class ControlLimit
{
    public int ControlLimitId { get; set; }
    public string ControlLimitDesc { get; set; }
    public float ControlLimitLow { get; set; }
    public float ControlLimitHigh { get; set; }
} 

private void OnEndEdit(object sender, DataGridCellEditEndingEventArgs e)
{
    //Get the cell info, which is old, then assign the new info 
    var cellInfo = (ControlLimit)(((System.Windows.Controls.DataGrid) sender).CurrentCell).Item;
    var editingElementPram = float.Parse(((System.Windows.Controls.TextBox) e.EditingElement).Text);
    if (e.Column.Header.ToString() == "Low Limit")
        cellInfo.ControlLimitLow = editingElementPram;
    else if (e.Column.Header.ToString() == "High Limit")
        cellInfo.ControlLimitHigh = editingElementPram;
    var controlLimitId = ControlLimit.UpdateControlLimitParameters(cellInfo);
}

答案 2 :(得分:0)

以防万一,我使用WPF DataGridCell中的单元格Content属性来获取值。这是一个单元格为文本类型的示例:

pager.setAdapter(adptr);
pager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);