获取单击按钮时第三列的单元格值

本文关键字:三列 单元格 按钮 单击 获取 | 更新日期: 2023-09-27 17:50:50

我有一个按钮在我的数据网格的第一列,当我点击它,我试图得到那行我点击按钮的第三列的单元格值。例如,我点击数据网格第3行的按钮我想要第3列的单元格值,第3行是int型。我该怎么做呢?

按钮的XAML:
<Control:DataGrid.Columns>              
    <Control:DataGridTemplateColumn>
        <Control:DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                 <Button Click="ShowHideDetailsClick" Foreground="Black">+</Button>
              </DataTemplate>
        </Control:DataGridTemplateColumn.CellTemplate>
     </Control:DataGridTemplateColumn>
<Control:DataGrid.Columns>

c#处理点击:

private void ShowHideDetailsClick(object sender, RoutedEventArgs e)
{
   //want to get cell value here
   System.Windows.Controls.Button expandCollapseButton = (System.Windows.Controls.Button)sender;
   DependencyObject obj = (DependencyObject)e.OriginalSource;
   while (!(obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow) && obj != null) obj = VisualTreeHelper.GetParent(obj);
        if (obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow)
        {
            if (null != expandCollapseButton && "+" == expandCollapseButton.Content.ToString())
            {
                (obj as ExtendedGrid.Microsoft.Windows.Controls.DataGridRow).DetailsVisibility = Visibility.Visible;
                expandCollapseButton.Content = "-";
            }
            else
            {
                (obj as ExtendedGrid.Microsoft.Windows.Controls.DataGridRow).DetailsVisibility = Visibility.Collapsed;
                expandCollapseButton.Content = "+";
            }
        }
 }

我使用一个DataTable来填充我的数据网格与从数据库检索的数据,见下面的代码:

public DataTable SourceTable
{
        get
        {
            return _sourceTable;
        }
        set
        {
            _sourceTable = value;
            OnPropertyChanged("SourceTable");
        }
 }
SourceTable = new DataTable();
SourceTable.Columns.AddRange(new DataColumn[]{
                new DataColumn("InputID", typeof(int)),
                new DataColumn("TraderID", typeof(string)),
                new DataColumn("TradeDate", typeof(DateTime)),
                new DataColumn("TradeTime", typeof(TimeSpan)),
                new DataColumn("ClientName", typeof(string)),
                new DataColumn("CurPair", typeof(string)),
                new DataColumn("Amnt", typeof(int)),
                new DataColumn("Action", typeof(string)),
                new DataColumn("ExecutedRate", typeof(decimal))
            });
DataRow rowSource = null;
var OpenTradesQuery = from qa in connection.QuickAnalyzerInputs
                                  where qa.TradeClosedDateTime == null
                                  select new
                                  {
                                      qa.InputID,
                                      qa.TraderID,
                                      qa.ClientTradedDate,
                                      qa.ClientTradedTime,
                                      qa.ClientName,
                                      qa.CurrencyPair,
                                      qa.TradedAmount,
                                      qa.Action,
                                      qa.ExecutedRate
                                  };
foreach (var rowObj in OpenTradesQuery)
                {
                    rowSource = SourceTable.NewRow();
                    SourceTable.Rows.Add(rowObj.InputID, rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.ClientName, rowObj.CurrencyPair, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate);
                }

然后在XAML中数据网格绑定SourceTable

获取单击按钮时第三列的单元格值

在做任何事情之前,我建议创建一个小类,它将包含关于数据库行的信息,这些行将填充数据网格。这将对代码中的一些功能有所帮助。这也将有助于实现我的答案。

类应该是这样的:

   public class PopulateData
   {
        public int       InputID;
        public String    TraderID;
        public DateTime  TradeDate;
        public TimeSpan  TradeTime;
        public string    ClientName;
        public string    CurPair;
        public int       Amnt;
        public string    Action;
        public decimal   ExecutedRate;
        public PopulateData(int iId, string tId, DateTime date, TimeSpan tTime, string cName, string curPair, int amnt, string Act, decimal ExRate)
        {
          InputID   = iId; 
          TraderID  = tId;
          TradeDate = date;
          TradeTime  = tTime;
          ClientName = cName;
          CurPair    = curPair;
          Amnt       = amnt;
          Action     = Act;
          ExecutedRate = ExRate;
        }
   }

当你填充SourceTable时,像这样做:

  foreach (var rowObj in OpenTradesQuery)
  {
     rowSource = SourceTable.NewRow();
     SourceTable.Rows.Add(new PopulateData( rowObj.InputID, rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.ClientName, rowObj.CurrencyPair, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate));
  }

当你得到与你的按钮(DataGridRow)对应的行后,它几乎完成了:现在你必须得到行里面的内容;a PopulateData实际上

    private void ShowHideDetailsClick(object sender, RoutedEventArgs e)
    {
      System.Windows.Controls.Button expandCollapseButton =  (System.Windows.Controls.Button)sender;
      DependencyObject obj = (DependencyObject)e.OriginalSource;
     while (!(obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow) && obj != null) obj = VisualTreeHelper.GetParent(obj);
     //Get your entire row view here
       if(obj != null && obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow)
       {
          DataGridRow d = obj as DataGridRow; //Convert into DataGridRow
          PopulateData st = d.Item as PopulateData; //Get the PopulateData
          if (st.TraderID == "what you wants") //Or something else in the Class
          {
            // some stuff here
          } 
       }


        if (obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow)
        {
           if (null != expandCollapseButton && "+" == expandCollapseButton.Content.ToString())
           {
             (obj as ExtendedGrid.Microsoft.Windows.Controls.DataGridRow).DetailsVisibility = Visibility.Visible;
             expandCollapseButton.Content = "-";
           }
            else
            {
              (obj as ExtendedGrid.Microsoft.Windows.Controls.DataGridRow).DetailsVisibility = Visibility.Collapsed;
            expandCollapseButton.Content = "+";
            }
        }
     }

就是这样,让我知道它是否适合你。