123456789
123456789

Reputation: 15

add the new row client has entered to data source on radgirdview

I am new to using the telerik tools for winforms and I was wondering if anyone could help me to find the best way so that the client can add a new row on the radgrid and for it to show this change on the data source.

so far I have the radgrid set up so that the client can add a new row. I just need to bind it to the data source.

    private void radGridView1_Click(object sender, EventArgs e)
    {

        this.radGridView1.AllowEditRow = false;
        this.radGridView1.AllowAddNewRow = true;
        this.radGridView1.AllowDeleteRow = false;
        this.radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;

        this.radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
        radGridView1.EnableAlternatingRowColor = true;

    }

Upvotes: 0

Views: 717

Answers (2)

checho
checho

Reputation: 3120

RadGridView supports data binding to variety of data sources and CRUD operation will be handled for you automatically. Here you can find information on the supported data sources: Telerik UI for WinForms Documentation

And here is how to bind to a DataTable, where all CRUD operations work out of the box

RadGridView radGridView1 = new RadGridView();
this.Controls.Add(radGridView1);
radGridView1.Dock = DockStyle.Fill;

DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Rows.Add("value1", "value1");
table.Rows.Add("value2", "value2");

radGridView1.DataSource = table;

Here is also a design time tutorial.

Upvotes: 0

roemel
roemel

Reputation: 3297

Have a look at the UserAddedRow event for RadGridView. This is fired after the user added a new row to the grid. You could then add the new entries to a source list or data table.

List<Foo> _lSource = new List<Foo>();
DataTable _tSource = new DataTable();

private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
    Foo foo = new Foo();
    foo.Col1 = e.Row.Cells["col1"].Value.ToString();
    foo.Col2 = e.Row.Cells["col2"].Value.ToString();

    _lSource.Add(foo);
    _tSource.Rows.Add(e.Row.Cells["col1"].Value.ToString(), e.Row.Cells["col2"].Value.ToString());
}

I added both possibilities in the same code snippet. Just choose one (list or table). I just created a random class to show you an example. You can create your own classes and name the properties as you want. Just note that the column (col1 and col2 in my example) must exist, otherwise you'll get a NullReferenceException. If you're using DataTable you have to add the columns once before adding rows.

_tSource.Columns.Add("col1");
_tSource.Columns.Add("col2");

I also recommend not to update the RadGridView properties on a click event of RadGridView. Because it is enough to set those properties once. Now you set them every time you click in your grid view. Either move them to the Load event of your form or set it directly in designer properties.

private void Form_Load(object sender, EventArgs e)
{
    radGridView1.AllowEditRow = false;
    radGridView1.AllowAddNewRow = true;
    radGridView1.AllowDeleteRow = false;
    radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;

    radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
    radGridView1.EnableAlternatingRowColor = true;
}

Upvotes: 2

Related Questions