We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to add data from textbox to sfDataGrid using buttons ?

maybe this is my unanswered answer what i want, i want to add data in textbox to sfdatagrid using button.. If using the built in DataGridView what I want can be achieved with the following code :


        private void button1_Click(object sender, EventArgs e)

        {

            string NewData = textBox1.Text;

            dataGridView1.Rows.Add(NewData);

            textBox1.Clear();

        }


Attachment: WindowsFormsApp1_c79d406d.7z

1 Reply

SJ Sathiyathanam Jeyakumar Syncfusion Team March 10, 2023 06:07 PM UTC

Hi RIDWAN,

When using SfDataGrid, there is no direct option to add data to the rows of the grid. In SfDataGrid the rows will be inserted when we directly add it to the underlying datasource. We recommend setting the DataSource first and then adding the data at runtime.

Model

public class Model: INotifyPropertyChanged

{

 

    private string data;

    public string Data { get => data; set { data = value; RaisePropertyChanged(nameof(Data)); } }

    public event PropertyChangedEventHandler PropertyChanged;

 

 

    void RaisePropertyChanged(string propertyName)

    {

        if (this.PropertyChanged != null)

            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

}


DataSource property

private ObservableCollection<Model> _Source = new ObservableCollection<Model>();

public ObservableCollection<Model> DataSource

{

    get

    {

        return _Source;

    }

    set

    {

        _Source = value;

        RaisePropertyChanged("DataSource");

    }

}

public Form1()

{

    InitializeComponent();

    this.sfDataGrid1.DataSource = DataSource;

}

Runtime Data Adding scenario:

private void button1_Click(object sender, EventArgs e)

{

    string NewData = textBox1.Text;

    dataGridView1.Rows.Add(NewData);

    DataSource.Add(new Model() { Data = NewData });

    textBox1.Clear();

}


Regards,

Sathiyathanam


Attachment: WindowsFormsApp1_3e11afa0.zip

Loader.
Up arrow icon