locked
Get cell value from WPF toolkit datagrid to a textbox RRS feed

  • Question

  • how to get a cell value from datagrid in wpf toolkit to a textbox in the wpf page, when the datagrid selection changed, at runtime.

    In my datagrid I assigned the data from database at runtime (that is directly a table vlues from database using itemsource property  of datagrid)



    Pls say the answer
    Thursday, February 11, 2010 7:17 AM

Answers

  • Hi LukmanulHakeem,

    You can get the cell value like this.
    <StackPanel>
                <dg:DataGrid Name="dataGrid1" ItemsSource="{Binding}" CurrentCellChanged="dataGrid1_CurrentCellChanged">

                </dg:DataGrid>
                <TextBox Name="textBox1"></TextBox>
                <TextBox Name="textBox2"></TextBox>
            </StackPanel>

    public partial class Window1 : Window
        {
            private DataTable dt = new DataTable();
            public Window1()
            {
                InitializeComponent();
                dt.Columns.Add("Column1");
                dt.Columns.Add("Column2");
                dt.Rows.Add(1, "Item1");
                dt.Rows.Add(2, "Item2");
                dt.Rows.Add(3, "Item3");

                dataGrid1.DataContext = dt;
            }

            private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
            {
                DataRowView drv = dataGrid1.CurrentCell.Item as DataRowView;
                if (drv != null)
                {
                    textBox1.Text = drv.Row[0].ToString();
                    textBox2.Text = drv.Row[1].ToString();
                }
            }
        }
    If you have any questions about it, please feel free to tell me.

    Sincerely,
    Kira Qian

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Kira Qian Friday, February 19, 2010 6:27 AM
    Saturday, February 13, 2010 3:34 AM

All replies

  • Hi LukmanulHakeem,

    You can get the cell value like this.
    <StackPanel>
                <dg:DataGrid Name="dataGrid1" ItemsSource="{Binding}" CurrentCellChanged="dataGrid1_CurrentCellChanged">

                </dg:DataGrid>
                <TextBox Name="textBox1"></TextBox>
                <TextBox Name="textBox2"></TextBox>
            </StackPanel>

    public partial class Window1 : Window
        {
            private DataTable dt = new DataTable();
            public Window1()
            {
                InitializeComponent();
                dt.Columns.Add("Column1");
                dt.Columns.Add("Column2");
                dt.Rows.Add(1, "Item1");
                dt.Rows.Add(2, "Item2");
                dt.Rows.Add(3, "Item3");

                dataGrid1.DataContext = dt;
            }

            private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
            {
                DataRowView drv = dataGrid1.CurrentCell.Item as DataRowView;
                if (drv != null)
                {
                    textBox1.Text = drv.Row[0].ToString();
                    textBox2.Text = drv.Row[1].ToString();
                }
            }
        }
    If you have any questions about it, please feel free to tell me.

    Sincerely,
    Kira Qian

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Kira Qian Friday, February 19, 2010 6:27 AM
    Saturday, February 13, 2010 3:34 AM
  • Hi LukmanulHakeem,

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Sincerely,
    Kira Qian
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Friday, February 19, 2010 6:27 AM
  • Thanks a lot


    Now I want to add new row in the datagrid(WPF toolkit) when click on a button in the outside of the datagrid



    If anyone answer me it will be very helpful for me....

    I tried a lot, but it was unsuccessful
    Saturday, February 27, 2010 6:04 AM
  • Hi LukmanulHakeem,

    You can add a new row to the datasource of the DataGrid.
    dt.Rows.Add(dt.NewRow());

    Sincerely,
    Kira Qian
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Monday, March 1, 2010 9:15 AM
  • I Heart fully,

    Thank you for your nice Material

    warm regards,

    Ayyappan,

    Software Engineer ,

    Chennai, 

    Tamil Nadu.

    http://www.banyansoft.blogspot.com/

    Thursday, January 27, 2011 4:37 AM