Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3282186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:53:59+00:00 2026-05-17T19:53:59+00:00

i will try to be clear.. here i am working on a datagrid. in

  • 0

i will try to be clear.. here i am working on a datagrid. in datagrid one column is editable as i am passing DataGridTextColumn to it and holding the data when users enter data into it and writing back to database. i am saving to database using datagrid_celleditending event also i am using the datagridcelleditendingeventargs to do this. This is done and working fine. i have added new functionality such that instead of entering data in datagrid, users will enter in one textbox which will be given on top of datagrid and the on entering data in to cell i am synchronizing it with datagrid cell and i can see data in it (just like excel having one big bar on top and you can write the data there and also see in datagrid). i am doing this by using textbox_keyup event. now i wanted to fire the event of datagrid_celleditending event args. i tried using routed event implementation but its not working.

hope i am not confusing, please help..

Here is a chunk of code..

private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        DataRowView rowView = e.Row.Item as DataRowView;
        rowBeingEdited = rowView;
        //Here my logic to write to database will come
        .........
        .........      
   }

//The above event is working fine when i change in datagrid now i wanted to fire this when i enter data in the text box which is not a part of datagrid

   private void tb_Comments_KeyUp(object sender, KeyEventArgs e)
    {
        if (rowBeingEdited != null)
        {
            rowBeingEdited.Row["Comments"] = tb_Comments.Text;
           //here i wanted to go to the above event and fire it..how can i do it??
        }

     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:53:59+00:00Added an answer on May 17, 2026 at 7:53 pm

    What’s your SelectionUnit for the DataGrid set to? This works for SelectionUnit=”CellOrRowHeader”, otherwise some things must be changed a bit at the end. Anyway, the following code puts a cell in edit-mode and then commits it which fires the CellEditEnding event.

    public void EnterCellEditEndingForCell(DataGridColumn column, DataRowView dataRowView)
    {
        int selectedRowIndex = -1;
        // c_dataGrid.Items.IndexOf(dataRowView)
        // sometimes break and return -1.
        for (int i = 0; i < c_dataGrid.Items.Count; i++)
        {
            DataRowView rowView = c_dataGrid.Items[i] as DataRowView;
            if (rowView == dataRowView)
            {
                selectedRowIndex = i;
                break;
            }
        }
        int selectedColumnIndex = c_dataGrid.Columns.IndexOf(column);
        if (selectedRowIndex >= 0 && selectedColumnIndex >= 0)
        {
            DataGridCell dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
            if (dataGridCell == null)
            {
                c_dataGrid.ScrollIntoView(dataRowView);
                dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
            }
            dataGridCell.BringIntoView();
            dataGridCell.Focus();
            c_dataGrid.SelectedCells.Clear();
            DataGridCellInfo info = new DataGridCellInfo(dataGridCell);
            c_dataGrid.SelectedCells.Add(info);
            c_dataGrid.BeginEdit();
            c_dataGrid.CommitEdit(DataGridEditingUnit.Cell, true);
        }
    }
    

    DataGridHelper.cs if you don’t have an implementation of it

    static class DataGridHelper
    {
        static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }
    
        static public DataGridCell GetCell(DataGrid dg, int row, int column)
        {
            DataGridRow rowContainer = GetRow(dg, row);
    
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
    
                // try to get the cell but it may possibly be virtualized
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
                return cell;
            }
            return null;
        }
    
    
        static public DataGridRow GetRow(DataGrid dg, int index)
        {
            DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                // may be virtualized, bring into view and try again
                dg.ScrollIntoView(dg.Items[index]);
                row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }
    }
    

    UPDATE
    If you have only one cell selected (which sounds like the case) than you can do this.

    private void tb_Comments_KeyUp(object sender, KeyEventArgs e) 
    { 
        if (rowBeingEdited != null) 
        { 
            rowBeingEdited.Row["Comments"] = tb_Comments.Text; 
            DataGridColumn columnBeingEdited= GetActiveDataGridColumn();
            EnterCellEditEndingForCell(columnBeingEdited, rowBeingEdited);
        } 
     } 
    
    private DataGridColumn GetActiveDataGridColumn()
    {
        if (c_dataGrid.SelectedCells.Count != 1)
        {
            return null;
        }
        return c_dataGrid.SelectedCells[0].Column;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an algorithm that will try to pick out, given an
this is my first time to post a question here, I will try to
I'm fairly new to C# but I will try to make this quick! ;)
This might be a little hard to explain, but I will try. I want
I'm try to develop a regex that will be used in a C# program..
I am about to try and automate a daily build, which will involve database
Will limiting a query to one result record, improve performance in a large(ish) MySQL
I have one more question(first one is here: html, css with center div problem
Will the code below work if the clock on the server is ahead of
Will I have to pay again? I have about 9 months left before renewal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.