本文整理汇总了C#中System.Windows.Controls.DataGrid.SelectAllCells方法的典型用法代码示例。如果您正苦于以下问题:C# DataGrid.SelectAllCells方法的具体用法?C# DataGrid.SelectAllCells怎么用?C# DataGrid.SelectAllCells使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.DataGrid
的用法示例。
在下文中一共展示了DataGrid.SelectAllCells方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportToExcel
public static void ExportToExcel(DataGrid dgDisplay)
{
dgDisplay.SelectAllCells();
dgDisplay.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dgDisplay);
String resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
String result = (string)Clipboard.GetData(DataFormats.Text);
dgDisplay.UnselectAllCells();
SaveFileDialog save = new SaveFileDialog();
save.Filter = "xls|*.xls";
save.ShowDialog();
if (!string.IsNullOrEmpty(save.FileName))
{
using (StreamWriter writer = new StreamWriter(save.FileName))
{
writer.WriteLine(result);
}
}
MessageBox.Show(" Exporting DataGrid data to Excel file created");
}
示例2: DataGridtoDataTable
//public static Run LoadStringTo(RichTextBox rtb, string tx)
//{
// if (tx == "") return null;
// TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
// MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(tx));
// textRange.Load(ms, DataFormats.Rtf);
// FlowDocument fd = rtb.Document;
// System.Windows.Documents.Paragraph pr = fd.Blocks.First() as System.Windows.Documents.Paragraph;
// return null;
//}
//public static ComboBoxItem FindComboItem(ComboBox combo, string text)
//{
// ComboBoxItem cit = new ComboBoxItem();
// foreach (var vit in combo.Items)
// {
// ComboBoxItem c = (ComboBoxItem)vit;
// if (vit.ToString() == text)
// cit = c;
// }
// return cit;
//}
public static DataTable DataGridtoDataTable(DataGrid dg)
{
dg.SelectAllCells();
dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
dg.UnselectAllCells();
String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
string[] Lines = result.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
//1st row must be column names; force lower case to ensure matching later on.
for (int i = 0; i < Cols; i++)
dt.Columns.Add(Fields[i].ToUpper(), typeof(string));
DataRow Row;
for (int i = 1; i < Lines.GetLength(0) - 1; i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt.NewRow();
for (int f = 0; f < Cols; f++)
{
Row[f] = Fields[f];
}
dt.Rows.Add(Row);
}
return dt;
}