本文介绍了报告出现时如何启用datagridcell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
推荐答案
<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 :
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);
}
...
...
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));
}
}
这篇关于报告出现时如何启用datagridcell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!