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

时间:2014-03-18 13:07:43

标签: c# wpf datagrid

我的数据网格的第一列中有一个按钮,当我点击它时,我试图获取该行第3列的单元格值,然后单击该按钮。所以例如我单击我的datagrid第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中,datagrid绑定SourceTable

1 个答案:

答案 0 :(得分:2)

在此之前,我建议创建一个小类,它将包含有关数据库行的信息,这些信息将填充Datagrid。这将帮助您使用代码中的某些工具。这也将有助于实现我的答案。

班级将是这样的:

   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)对应的行后,它几乎完成了: 你现在必须得到行内的东西;实际上是一个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 = "+";
            }
        }
     }

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