#c_sharp #wpf
Пытаюсь сделать запись содержимого DataGrid в html-таблицу.
Нашел пример кода, но для DataGridView.
private StringBuilder DataGridtoHTML(DataGridView dg)
{
StringBuilder strB = new StringBuilder();
//create html & table
strB.AppendLine("<" +
"table border='1' cellpadding='0' cellspacing='0'>");
strB.AppendLine("");
//cteate table header
for (int i = 0; i < dg.Columns.Count; i++)
{
strB.AppendLine("" +
dg.Columns[i].HeaderText + " ");
}
//create table body
strB.AppendLine(" ");
for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine(" ");
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
strB.AppendLine("" +
dgvc.Value.ToString() + " ");
}
strB.AppendLine(" ");
}
//table footer & end of html file
strB.AppendLine(" ");
return strB;}
Проблема в том, что у DataGrid нет свойства Rows как у DataGridView.
Подскажите, как перебрать таким же образом строки для DataGrid?
Ответы
Ответ 1
var rowIndex = dataGrid.SelectedIndex; var row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(selectedIndex); или dataGrid.SelectedIndex = 3; var selectedRow= (DataRowView)dataGrid.SelectedItem;Ответ 2
for (int i = 0; i < dataGrid.Items.Count; i++) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i); }
Комментариев нет:
Отправить комментарий