I am working on windows form application.
It contains two form MainForm - Test and childForm - Search
Search form has dataGridView control.It contains columns like SrNo, TypeNo, TestEngineer and Date. And Test form contains textBoxes tb_SerialNo, tb_TypeNo, tb_TestEngineer, datTimePicker for date.
My main Question is when I select Row from datagridview, I want SrNo column value from that row in textbox tb_SerialNo. Same for all.
I wrote following code. But it gives me only SrNo value in tb_SerialNo. but I didn't get TypeNo, TestEnginer and date values in respective textboxes. I am unable to find what i am missing. Please help me to resolve this issue. thanks in advance.
mainForm - Test Code
private void SearchToolStripMenuItem_Click(object sender, EventArgs e)
{
SearchTest Search = new SearchTest(dt);
Search.ShowDialog();
dt.DefaultView.RowFilter = "";
tb_SerialNo.Text = Search.SerialNo;
Search.typeNo = tb_TypeNo.Text;
Search.TestEngineer = tb_TestEngineer.Text;
Search.Date = dateTimePicker1.Text;
}
ChildForm -SearTest Code :
public partial class SearchTest : Form
{
public SearchTest(DataTable TestData)
{
InitializeComponent();
dataGridView1.DataSource = TestData;
this.dataGridView1.Sort(this.dataGridView1.Columns["SrNo"], ListSortDirection.Ascending);
}
private void btn_Search_Click(object sender, EventArgs e)
{
string str = dateTimePicker1.Text;
string str1 = dateTimePicker2.Text;
DateTime date = DateTime.ParseExact(str, "MM/dd/yyyy", CultureInfo.GetCultureInfo("en-GB"));
DateTime date1 = DateTime.ParseExact(str1, "MM/dd/yyyy", CultureInfo.GetCultureInfo("en-GB"));
string dtFilter = string.Format("[Date] >= '{0} ' AND [Date] <= '{1} '", date.ToString("MM/dd/yyyy"), date1.ToString("MM/dd/yyyy"));
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = dtFilter;
}
private void tb_SearchSrNo_TextChanged(object sender, EventArgs e)
{
try
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = String.IsNullOrEmpty(tb_SearchSrNo.Text) ?
"SrNo IS NOT NULL" :
String.Format("SrNo LIKE '{0}%' OR SrNo LIKE '{1}%' OR SrNo LIKE '{2}%'", tb_SearchSrNo.Text, tb_SearchSrNo.Text, tb_SearchSrNo.Text);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SearchTest_Load(object sender, EventArgs e)
{
groupBox1.Enabled = false;
groupBox3.Enabled = false;
cb_Filter.MouseWheel += new MouseEventHandler(cb_Filter_MouseWheel);
}
private void cb_Filter_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Filter.SelectedIndex == 0)
{
groupBox1.Enabled = true;
}
else
{
groupBox3.Enabled = true;
}
}
void cb_Filter_MouseWheel(object sender, MouseEventArgs e)
{
((HandledMouseEventArgs)e).Handled = true;
}
private string SrNo;
private string TypeNo;
private string TestEng;
private string date;
public string SerialNo
{
get { return SrNo; }
set { SrNo = value; }
}
public string typeNo
{
get { return TypeNo; }
set { TypeNo = value; }
}
public string TestEngineer
{
get { return TestEng; }
set { TestEng = value; }
}
public string Date
{
get { return date; }
set { date = value; }
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
SrNo = dataGridView1.CurrentRow.Cells["SrNo"].Value.ToString();
TypeNo = dataGridView1.CurrentRow.Cells["TypeNo"].Value.ToString();
TestEng = dataGridView1.CurrentRow.Cells["TestEngineer"].Value.ToString();
date = dataGridView1.CurrentRow.Cells["Date"].Value.ToString();
}
}
You can change the property definitions to something like this:
public string SomeProperty
{
get
{
string value = null;
if(BindingContext[dataGridView1.DataSource].Current !=null)
{
var r = ((DataRowView)BindingContext[dataGridView1.DataSource].Current).Row;
value = r.Field<string>("SomeDataColumn");
}
return value;
}
}
This way, the SomeProperty
will always return the value of SomeDataColumn
from the active row.