Appu
Appu

Reputation: 11

Auto complete text boxes from datagrid - C#

I am beginner and I am developing an application in Windows CE 6 in Visual Studio 2008. I have datagrid contains user details, some text boxes are placed below the grid to edit the user details. Now I want to fill these text boxes when user clicks on the data grid. I have tried every thing and results from interent are based on "datagridview". What I need is How to fill textboxes from a DATAGRID not DATAGRIDVIEW !!

This is what I have tried

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

I know this code is wrong, because it fill textbox-Name Edit from the current row and current cell. I want fill all the textboxes from the current row. Someone please help me! I AM IN DEEP TROUBLE NOW!!!

Upvotes: 0

Views: 1022

Answers (2)

Appu
Appu

Reputation: 11

I have used a short cut, Hope this will help others too...

        int row = dgShowData.CurrentCell.RowNumber;

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

Upvotes: 1

MRebai
MRebai

Reputation: 5474

If you want to retrieve cell value then try the code below :

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

For 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;

Upvotes: 0

Related Questions