Showing posts with label Windows Forms. Show all posts
Showing posts with label Windows Forms. Show all posts

Thursday, February 11, 2010

Search in Datagridview


                 This is a sample for search datagridview for example.  The user will  type the employee number into the textbox and click on the search button.  then we want the focus to move to the row with the employee number .
private void SelectGridRow(int id)
{
 foreach (DataGridViewRow row in dataGridView1.Rows)
 {
  //assume col 0 as emp column
  if (dataGridView1.Rows[row.Index].Cells[0].Value.Equals(id))
  {
   dataGridView1.Rows[row.Index].Selected = true;
   dataGridView1.Rows[row.Index].DefaultCellStyle.BackColor = Color.Coral;
   dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
   return;
  }
 }

Wednesday, November 4, 2009

Update windows form from child to parent

we are opening child forms and doing some calculation.after finished the calculation in child form. whatever changes we made it should be reflected immediately in the main form.for that I am used event handler to update the main form.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//Wired Up Here
frm2.RefreshMain += new EventHandler(frm2_RefreshMain);
frm2.ShowDialog();
}
void frm2_RefreshMain(object sender, EventArgs e)
{
//Write your logic here to update datagridview
}

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);
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();
}

}

//Code For Form 2

public partial class Form2 : Form{

//Declare event handler here
public event EventHandler RefreshMain;
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

}

protected void UpdateMain()
{
if (RefreshMain != null)
RefreshMain(this, EventArgs.Empty);
}

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
UpdateMain();
}

}

Friday, October 30, 2009

How To DataGridview To Dataset Code Sample.

      We need to create the datacolumn from the datagridview column.Add created Column in to datatable .Add the datatable in to dataset.loop through the datagridview add data to datatable .

DataTable table = new DataTable("mytable");

DataColumn column;

DataRow row;

DataSet dataset = new DataSet();

column = new DataColumn();

column.DataType = dataGridView1.Columns[0].ValueType;

column.ColumnName = dataGridView1.Columns[0].Name;

table.Columns.Add(column);

column = new DataColumn();

column.DataType = dataGridView1.Columns[1].ValueType;

column.ColumnName = dataGridView1.Columns[1].Name;

table.Columns.Add(column);

dataset.Tables.Add(table);

for (int rowCount = 0; rowCount < dataGridView1.Rows.Count - 1; rowCount++)

{

row = table.NewRow();

row[0] =Convert.ToInt32(dataGridView1.Rows[rowCount].Cells[0].Value);

row[1] = dataGridView1.Rows[rowCount].Cells[1].Value.ToString();

table.Rows.Add(row);

}

return dataset;