Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how can i a Button column to a Datagrid programatically. I want to do this through code in codebehind file.<pre>if the status shows "REPORTED" then enable the datagridbutton in wpf</pre>


int rowIndex = 10;
var rowData = dataGrid1.Items[rowIndex] as DataRowView;
rowData["LinkRPT"] = "";
dataGrid1.Columns[10].Visibility = Visibility.Visible;
SqlConnection con = new SqlConnection("Data Source=WINCTRL-TNT3FMR\\SQLEXPRESS;Initial    
            Catalog=Roamani;Integrated Security=True");
if (dataGrid1.SelectedItems.Count > 0)
{
    for (int i = 0; i < dataGrid1.SelectedItems.Count; i++)
    {
        System.Data.DataRowView selectedFile= (System.Data.DataRowView)dataGrid1.SelectedItem;//Row is detected and stored in selectedfile object
        string str = Convert.ToString(selectedFile.Row.ItemArray[10]);
        string sts = Convert.ToString(selectedFile.Row.ItemArray[9]);
 
        string query = "SELECT Report FROM StudyTable WHERE StudyUID='" + str + "' AND status='3'";
        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        try
        {
            while (rdr.Read())
            {
                query = rdr.GetString(0);
                //dataGrid1.Columns[10] = query;
                OpenReport(query);
            }
        }
        catch (Exception ex)
        {
            ReallySimpleLog.ReallySimpleLog.WriteLog(ex);
        }
    }
}
con.Close();
Posted
Comments
Member 10889548 17-Jun-14 6:54am    
Am stuck here,please help to do

1 solution

In xaml side add this part :

XML
<DataTemplate x:Key="columnTemplate">
      <Button x:Uid="rowButton" Content="Do some thing" Margin="2" IsEnabled="{Binding IsActive}"/>
</DataTemplate>



In server side do some thing like this :

C#
private void FillDate()
{
      ObservableCollection<mydate> data = new ObservableCollection<mydate>();
      for (int i = 1; i <= 20; i++)
          data.Add(new MyDate()
          {
                ID = i,
                Name = "Name" + i,
                Family = "Family" + i,
                IsActive = true
          });

      dataGrid.ItemsSource = data;

      DataGridTemplateColumn tColumn = new DataGridTemplateColumn();
      tColumn.CellTemplate = (DataTemplate)this.FindResource("columnTemplate");
      tColumn.Header = "Button column";
      dataGrid.Columns.Add(tColumn);
}


...

C#
public class MyDate : INotifyPropertyChanged
    {
        public MyDate() { }

        private int _iD;
        public int ID
        {
            get { return _iD; }
            set
            {
                _iD = value;
                Notify("ID");
            }
        }

        private string _name = "";
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                Notify("Name");
            }
        }

        private string _family = "";
        public string Family
        {
            get
            {
                return _family;
            }
            set
            {
                _family = value;
                Notify("Family");
            }
        }

        private bool _isActive = false;
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                Notify("IsActive");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void Notify(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
 
Share this answer
 
v2
Comments
Member 10889548 17-Jun-14 8:41am    
how am edit it in my code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900