1

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

}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
User0804
  • 77
  • 2
  • 12
  • You should not edit the question and totally change it. It makes the existing answers and comments nonsense. If you have any problem regarding to the comments, continue the discussion in the comments or ask a new question. I reverted the question back to the original version. – Reza Aghaei Jun 25 '18 at 10:45
  • For a data-oriented application, you will find [this post](https://stackoverflow.com/q/38165043/3110834) and [this one](https://stackoverflow.com/a/37824444/3110834) useful. – Reza Aghaei Jun 25 '18 at 10:49
  • Hope you get the point of my comment. It's completely OK to edit questions, but you should not make it a totally new question by changing it :) – Reza Aghaei Jun 25 '18 at 10:57

3 Answers3

1

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

Try using SelectedRow instead.
The difference between SelectedRow and CurrentRow:

  • CurrentRow is where the mouse cursor and is selected by the system.
  • SelectedRow is the row selected in the datagridview and is always selected by the user.

Here's an example code:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedCells.Count > 0)
    {
        int index = dataGridView1.SelectedCells[0].RowIndex;
        DataGridViewRow selectedRow = dataGridView1.Rows[index];

        SrNo = selectedRow.Cells["SrNo"].Value.ToString();
        TypeNo = selectedRowCells["TypeNo"].Value.ToString();
        TestEng = selectedRowCells["TestEngineer"].Value.ToString();
        date = selectedRowCells["Date"].Value.ToString();
    }
}
  • Your description about [current row](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentrow.aspx) is wrong. Current row is the rows which contains the current active cell. And also it's better to use Current row. – Reza Aghaei Jun 25 '18 at 09:26
  • 1
    @Joseph Joshua Anggita - The above code is not working. It gives me the same result as I mentioned in my question above. – User0804 Jun 25 '18 at 10:44
0

I got the answer and my issue is resolved. I found my mistake in mainForm - Test code. I used properties in wrong way for typeNo, TestEngineer and date except for Serial number. Thats why I didn't get values in respective textboxes.

The Corrected 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;
    tb_TypeNo.Text = Search.typeNo;
    tb_TestEngineer.Text = Search.TestEngineer;
    dateTimePicker1.Text = Search.Date;
}

No need to do any change in Search Form.

User0804
  • 77
  • 2
  • 12