从datagrid自动完成文本框 - C#

时间:2014-10-16 08:24:11

标签: c# datagridview windows-ce

我是初学者,我正在Visual Studio 2008中开发Windows CE 6中的应用程序。我有datagrid包含用户详细信息,一些文本框放在网格下面以编辑用户详细信息。现在,我想在用户点击数据网格时填写这些文本框。我尝试了所有的东西,互联网的结果都基于“datagridview”。我需要的是如何从 DATAGRID 填充文本框而不是 DATAGRIDVIEW !!

这就是我试过的

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

我知道这段代码是错误的,因为它填充了当前行和当前单元格中的textbox-Name Edit。我想填充当前行的所有文本框。有人请帮帮我!我现在深陷困境!!!

2 个答案:

答案 0 :(得分:1)

我使用了捷径,希望这对其他人也有帮助......

        int row = dgShowData.CurrentCell.RowNumber;

        txtNameEdit.Text = string.Format("{0}", dgShowData[row, 0]);
        txtNickNameEdit.Text = string.Format("{0}", dgShowData[row, 1]);

答案 1 :(得分: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;
        }

修改

对于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;