This is a migrated thread and some comments may be shown as answers.

Adding rows in gridview after filter for blank rows

3 Answers 526 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lokesh
Top achievements
Rank 1
Lokesh asked on 25 Sep 2014, 01:19 PM
I am applying filter to a radgridview as
 ((DataTable)Grid.DataSource).DefaultView.RowFilter = TxtBxExpression.Text;
                ((DataTable)Grid.DataSource).DefaultView.AllowNew = true;
                Grid.MasterTemplate.Refresh();
                Grid.MasterView.Refresh();  

After that when I am trying to insert a row as 
Grid.Rows.Add();

It is inserting a row at the end but it is without any columns and not even dropdowns of the rows are selectable so that I can change the row values and the update the data.So I want a row that has columns and its dropdowns are editable.So how can I do that?

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Sep 2014, 06:22 AM
Hello Lokesh,

Thank you for writing.

It seems that you use the RadGridView in bound mode. For this reason it is recommended to add new rows to the DataSource itself, not to the RadGridView.Rows collection directly which is relevant for unbound mode. Here is a sample code snippet demonstrating the approach for adding new rows in bound grid:
DataTable dt = new DataTable();
 
public enum DeliveryType
{
    Delivery,
    PickUp,
}
 
public Form1()
{
    InitializeComponent();
    
    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("DeliveryType", typeof(DeliveryType));
 
    for (int i = 0; i < 20; i++)
    {
        dt.Rows.Add(i, "Item" + i, DeliveryType.Delivery);
    }
 
    this.radGridView1.DataSource = dt;
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}
 
private void radButton1_Click(object sender, EventArgs e)
{
    ((DataTable)radGridView1.DataSource).DefaultView.RowFilter = "Name LIKE '%1%'";
    ((DataTable)radGridView1.DataSource).DefaultView.AllowNew = true;
    radGridView1.MasterTemplate.Refresh();
    radGridView1.MasterView.Refresh();
 
    dt.Rows.Add(100, "New1", DeliveryType.PickUp);
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Lokesh
Top achievements
Rank 1
answered on 30 Sep 2014, 12:13 PM
Hi Desislava,
     I tried what you have replied but the same problem arises again as the row which is added is also filtered and it is uneditable or invisible and visible on removing of filter.
I want a row to be added as with the following code :
((DataTable)(this.Grid.DataSource)).DefaultView.AddNew();
but the problem with it is that I am not able to add arguments to it.
and also u can try as :((DataTable)radGridView1.DataSource).DefaultView.RowFilter = "Id < 11"; in your code the same thing is happening with me.


0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Oct 2014, 08:38 AM
Hello Lokesh,

Thank you for writing back.

Please refer to the DataView.AddNew method article in the msdn documentation. The AddNew() method does not accept parameters, but you can change the values in the DataRow:
private void radButton1_Click(object sender, EventArgs e)
{
    ((DataTable)radGridView1.DataSource).DefaultView.RowFilter = "Id < 11";
    ((DataTable)radGridView1.DataSource).DefaultView.AllowNew = true;
  
    DataRowView rowView = ((DataTable)(this.radGridView1.DataSource)).DefaultView.AddNew();
 
    // Change values in the DataRow.
    rowView["Id"] = 5;
    rowView["Name"] = "NEW";
    rowView.EndEdit();
 
    radGridView1.MasterTemplate.Refresh();
    radGridView1.MasterView.Refresh();
}

I would like to note that if you already have an applied filter, the new row is normally to be filtered if it does not meet the filter condition. In the code snippet above, the new row will be visible. However, if the "Id" is 12, the new row will not be visible and this behavior is correct.

If it does not meet your specific filtering requirement, feel free to use our custom filtering functionality.

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Lokesh
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Lokesh
Top achievements
Rank 1
Share this question
or