Search code examples
c#winformsdatagridviewdatatabletextbox

How to get row's column value from datatable to textbox after double click on datagridview row without using properties


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 double click Row from datagridview, I want SrNo column value from that row in textbox tb_SerialNo. Same for all.

I resolved this issue by using properties.But my senior said, not to use properties if you have datatable. Please help me to solve this issue. Thanks in advance.

mainForm - Test Code

private void SearchToolStripMenuItem_Click(object sender, EventArgs e)
{
    SearchTest Search = new SearchTest();
    Search.ShowDialog();
    dt.DefaultView.RowFilter = "";

}

ChildForm - SearchTest:

public partial class SearchTest : Form
{
    public SearchTest()
    {
        InitializeComponent();
        dataGridView1.DataSource = Test.dt;
        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;

        string dtFilter = string.Format("[Date] >= '{0} ' AND [Date] <= '{1} '", str, str1);
        (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 void dataGridView1_DoubleClick(object sender, EventArgs e)
    {

        this.Close();

    }

}

Solution

  • To get information from the dialog, you need to expose DataGridView or the selected DataRow.

    Using a property

    If you decide to expose a property rather than exposing DataGridView, first create a property:

    DataRow currentRow = null;
    public DataRow CurrentRow 
    {
        get 
        {
            if (dataGridView1.CurrentRow != null)
                currentRow = ((DataRowView)dataGridView1.CurrentRow.DataBoundItem).Row;
            return currentRow;
        }
    }
    

    Then use it this way:

    using (var searchForm = new SearchForm())
    {
        searchForm.ShowDialog();
        var currentRow = searchForm.CurrentRow;
        if(currentRow != null) 
        {
            //You can find fields from the data row here:
            //string someField = (string)currentRows["someField"];
            //string someField = currentRows.Field<string>("someField");
        }
    }
    

    Exposing DataGridView

    If for any reason you decided to expose DataGridView, in the designer of search form, set Modifiers property of the dataGridView1 to public and then use it this wayL

    using (var searchForm = new SearchForm())
    {
        searchForm.ShowDialog();
        DataRow currentRow = null;
        if (searchForm.dataGridView1.CurrentRow != null)
            currentRow = ((DataRowView)searchForm.dataGridView1.CurrentRow.DataBoundItem).Row;
        if(currentRow != null) 
        {
            //You can find fields from the data row here:
            //string someField = (string)currentRows["someField"];
            //string someField = currentRows.Field<string>("someField");
        }
    }
    

    Note

    If you need to detect if the user closed the dialog by double click or by pressing the close button, it's better to set dialog result when closing in double click:

    DialogResult = DialogResult.OK;
    

    Then when showing the dialog, check the dialog result first:

    if(searchForm.ShowDialog() == DialogResult.OK)
    {
        //The user closed the dialog by double click on row
    }