2014-10-16 4 views
0

Я начинаю, и я разрабатываю приложение в Windows CE 6 в Visual Studio 2008. У меня есть datagrid содержит данные пользователя, некоторые текстовые поля помещаются под сеткой, чтобы отредактировать детали пользователя. Теперь я хочу заполнить эти текстовые поля, когда пользователь нажимает на сетку данных. Я пробовал все, и результаты от interent основаны на «datagridview». Что мне нужно - Как заполнить текстовые поля от DATAGRID не DATAGRIDVIEW !!Автоматическое заполнение текстовых полей из datagrid - C#

Это то, что я попытался

int row = dgShowData.CurrentCell.RowNumber; 
int col = dgShowData.CurrentCell.ColumnNumber; 
txtNameEdit.Text = string.Format("{0}", dgShowData[row, col]); 

Я знаю, что этот код является неправильным, так как заполнить текстовое поле-Name Edit из текущей строки и текущей ячейки. Я хочу заполнить все текстовые поля из текущей строки. Кто-нибудь, пожалуйста, помогите мне! Я ЕСМЬ В НЕПРЕРЫВНОЙ ПРОБЛЕМЕ!

+0

Нет t уверен, но есть ли что-то вроде dgShowData.CurrentCell.Text или dgShowData.CurrentCell.Value ?? – User2012384

+0

Покажите нам, как вы заполняете свою сетку данных – Reniuz

+0

Datagrid заполняется из базы данных. SqlCeDataAdapter da = новый SqlCeDataAdapter (cmd); DataSet ds = new DataSet(); da.Fill (ds); dgShowData.DataSource = ds.Tables [0]; – Appu

ответ

0

Если вы хотите получить значение ячейки попробуйте код ниже:

private void dgShowData_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    DataGridRow row = GetSelectedRow(dgShowData); 
    int index = dgShowData.CurrentCell.Column.DisplayIndex; 
    DataGridCell columnCell = GetCell(dgShowData,row, index); 
    TextBlock c = (TextBlock)columnCell.Content; 
    txtNameEdit.Text = c.Text; 
} 

/// <summary> 
     /// Gets the selected row of the DataGrid 
     /// </summary> 
     /// <param name="grid">The DataGrid instance</param> 
     /// <returns></returns> 
     public static DataGridRow GetSelectedRow(this DataGrid grid) 
     { 
      return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem); 
     } 


/// <summary> 
     /// Gets the specified cell of the DataGrid 
     /// </summary> 
     /// <param name="grid">The DataGrid instance</param> 
     /// <param name="row">The row of the cell</param> 
     /// <param name="column">The column index of the cell</param> 
     /// <returns>A cell of the DataGrid</returns> 
     public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column) 
     { 
      if (row != null) 
      { 
       DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); 

       if (presenter == null) 
       { 
        grid.ScrollIntoView(row, grid.Columns[column]); 
        presenter = GetVisualChild<DataGridCellsPresenter>(row); 
       } 

       DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); 

       return cell; 
      } 
      return null; 
     } 

EDIT

Для WinForms:

DataGridCell currentCell; 

string currentCellData; 

// Get the current cell. 

currentCell = dgShowData.CurrentCell; 

// Get the current cell's data. 

currentCellData = dgShowData[currentCell.RowNumber,currentCell.ColumnNumber].ToString(); 

// Set the TextBox's text to that of the current cell. 

txtNameEdit.Text = currentCellData; 
+0

Вы работаете с МОФ или WinForms –

+0

Создание приложения Windows CE, Winforms – Appu

+0

Я думал, что вы работаете с WPF –

1

Я использовал короткую стрижку, Надеюсь, что это поможет другие тоже ...

 int row = dgShowData.CurrentCell.RowNumber; 

     txtNameEdit.Text = string.Format("{0}", dgShowData[row, 0]); 
     txtNickNameEdit.Text = string.Format("{0}", dgShowData[row, 1]); 
Смежные вопросы