【问题标题】:Get the cell value of the third column on button click单击按钮获取第三列的单元格值
【发布时间】:2014-04-24 04:37:19
【问题描述】:

我的数据网格的第一列中有一个按钮,当我单击它时,我试图获取该行第三列的单元格值,我单击了该按钮。因此,例如,我单击数据网格第 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

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    首先,我建议创建一个包含有关数据库行的信息的小类,该类将填充数据网格。这将帮助您在代码中使用一些功能。这也将有助于实施我的答案。

    类将是这样的:

       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 = "+";
                }
            }
         }
    

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

    【讨论】:

    • 感谢您的回复,我在行项目 var row = (DataRowView)obj.Row.Item;它说“'System.Windows.DependencyObject'不包含'Row'的定义,并且找不到接受'System.Windows.DependencyObject'类型的第一个参数的扩展方法'Row'(您是否缺少使用指令还是程序集参考?)”@Lionnel Afangbedjee
    • 哦,对不起,我已经编辑了我的代码以避免这种情况。告诉我这次它是否有效:) @kknaguib
    • 这一行 List st = d.Item as List;给我一个例外“对象引用未设置为对象的实例”@Lionnel Afangbedjee
    • 您将什么类型的集合添加到 Datagrid 中?我的意思是,这都是 int 数据吗?你能用那个编辑你的问题吗?我在示例代码中设置了 List 因为我不知道您使用的是哪种集合
    • 好的,希望这个是正确的 :) 我已经编辑了我的答案,试着让我知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多