Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Filter a datagridview from another form
 

Filter a datagridview from another form

Hello,

I am newbie in C# and I would like to ask a beginner question.
I have a formA where I have a datagridview, in this datagridview I added a menutrip menu. When I click on the menu item of the menutrip (Find), another form open proposing me to enter a value in order to filter my datagridview in the FormA.

This is the Code I have in the FormB (Find form)

private

void btnOK_Click(object sender, EventArgs e)

frmMAB frmBoard = new AppBrd.frmMAB();

foreach (Form frm in Application.OpenForms)

if (frm is AppBrd.frmMAB)

frm.BringToFront();

string strFilter = GetFilter(this.txtSearch.Text);

DataGridView dgv = (DataGridView)frmBoard.dgMalMbr;

DataTable)dgv.DataSource).DefaultView.RowFilter=strFilter; break;

}

}

}


I have a error telling me "System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object.."

Could someone help me on that?

Thank you in Advance,

Rangi

Rangi Tanis  Friday, September 04, 2009 10:36 AM
Hi,
Create 2 Forms .add datagridview to form 1 add textbox to form 2
check this code.i hope it will help you

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FilterGrid += new Form2.FilterEvent(frm2_FilterGrid);
frm2.ShowDialog();
}

void frm2_FilterGrid(object sender, FilterEventArgs e)
{
DataTable dtCust = dsCust.Tables[0];

EnumerableRowCollection<DataRow> query = from cust in dtCust.AsEnumerable()
where cust.Field<Int32>("CustID") == e.FilterItem
select cust;

DataView view = query.AsDataView();
dataGridView1.DataSource = view;
dataGridView1.AutoResizeColumns();
}



private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;

// Add the column to the DataColumnCollection.
table.Columns.Add(column);

//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

dataset.Tables.Add(table);

// Create three sets of DataRow objects, 
// five rows each, and add to DataTable.
for (int i = 0; i <= 100; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;

}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}

private void Form1_Load(object sender, EventArgs e)
{
BindData();
}



public partial class Form2 : Form
{
public delegate void FilterEvent(object sender, FilterEventArgs e);
public event FilterEvent FilterGrid;
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

}

public void FilterGridView(string filterText)
{

FilterEventArgs args = new FilterEventArgs(Convert.ToInt32(filterText));
FilterGrid(this, args);

}

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
FilterGridView(textBox1.Text.Trim());
}
}
public class FilterEventArgs : EventArgs
{
public FilterEventArgs(int item)
{
this.filterItem = item;
}
private int filterItem;

public int FilterItem
{
get { return filterItem; }
set { filterItem = value; }
}
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Saturday, September 05, 2009 8:23 AM
Hi,
Create 2 Forms .add datagridview to form 1 add textbox to form 2
check this code.i hope it will help you

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FilterGrid += new Form2.FilterEvent(frm2_FilterGrid);
frm2.ShowDialog();
}

void frm2_FilterGrid(object sender, FilterEventArgs e)
{
DataTable dtCust = dsCust.Tables[0];

EnumerableRowCollection<DataRow> query = from cust in dtCust.AsEnumerable()
where cust.Field<Int32>("CustID") == e.FilterItem
select cust;

DataView view = query.AsDataView();
dataGridView1.DataSource = view;
dataGridView1.AutoResizeColumns();
}



private DataSet CreateDataSet()
{
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
DataSet dataset = new DataSet();
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CustID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;

// Add the column to the DataColumnCollection.
table.Columns.Add(column);

//// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CustName";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

dataset.Tables.Add(table);

// Create three sets of DataRow objects, 
// five rows each, and add to DataTable.
for (int i = 0; i <= 100; i++)
{
row = table.NewRow();
row["CustID"] = i;
row["CustName"] = "Item " + i;
table.Rows.Add(row);
}
return dataset;

}
DataSet dsCust;
private void BindData()
{
dsCust = CreateDataSet();
dataGridView1.DataSource = dsCust.Tables[0];
}

private void Form1_Load(object sender, EventArgs e)
{
BindData();
}



public partial class Form2 : Form
{
public delegate void FilterEvent(object sender, FilterEventArgs e);
public event FilterEvent FilterGrid;
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

}

public void FilterGridView(string filterText)
{

FilterEventArgs args = new FilterEventArgs(Convert.ToInt32(filterText));
FilterGrid(this, args);

}

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
FilterGridView(textBox1.Text.Trim());
}
}
public class FilterEventArgs : EventArgs
{
public FilterEventArgs(int item)
{
this.filterItem = item;
}
private int filterItem;

public int FilterItem
{
get { return filterItem; }
set { filterItem = value; }
}
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Saturday, September 05, 2009 8:23 AM

You can use google to search for other answers

Custom Search

More Threads

• How to tell when cursor is in the new row
• I have to remove a databinding at runtime
• how to enable Sorting in Gridview for which datasource is ArraryList
• removing a row from a datatable by clicking the row in the datagridview it belongs to
• Getting current row
• Databinding of a Textbox
• DataGridView leak due to UserPreferenceChangedEventHandler
• Removing controls from Array List
• Filling a datagrid on Windows Form with Visual Studio 2003
• more on binding name/value