簡體   English   中英

從DataGrid自動完成文本框-C#

[英]Auto complete text boxes from datagrid - C#

我是新手,我正在Visual Studio 2008的Windows CE 6中開發應用程序。我的datagrid包含用戶詳細信息,一些文本框位於網格下方以編輯用戶詳細信息。 現在,我想在用戶單擊數據網格時填充這些文本框。 我已經嘗試了一切,interent的結果基於“ datagridview”。 我需要的是如何從DATAGRID而不是DATAGRIDVIEW填充文本框!

這就是我嘗試過的

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

我知道此代碼是錯誤的,因為它從當前行和當前單元格填充了文本框名稱編輯。 我要填充當前行中的所有文本框。 有人請幫助我! 我現在陷入困境!!!

我使用了捷徑,希望這也會對其他人有所幫助...

        int row = dgShowData.CurrentCell.RowNumber;

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

如果要檢索單元格值,請嘗試以下代碼:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM