如何在不使用属性的情况下双击 datagridview 行后将行的列值从数据表获取到文本框

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

我正在处理 windows 表格申请。

它包含两个表单 MainForm - Test 和 childForm - Search

搜索表单有 dataGridView control.It 包含 SrNo、TypeNo、TestEngineer 和 Date 等列。测试表单包含文本框 tb_SerialNo、tb_TypeNo、tb_TestEngineer、日期的 datTimePicker。

我的主要问题是,当我双击 datagridview 中的行时,我想要文本框 tb_SerialNo 中该行的 SrNo 列值。所有人都一样。

我用我学长说的properties.But解决了这个问题,如果你有数据表就不要使用属性。请帮我解决这个问题。提前致谢。

mainForm - 测试代码

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

    }

}

要从对话框中获取信息,您需要公开 DataGridView 或选定的 DataRow

使用 属性

如果您决定公开 属性 而不是公开 DataGridView,请先创建 属性:

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

然后这样使用:

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

公开 DataGridView

如果您出于任何原因决定公开 DataGridView,请在搜索表单设计器中将 dataGridView1Modifiers 属性 设置为 public然后这样使用L

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

备注

如果需要检测用户是否通过双击或按下关闭按钮关闭对话框,最好设置双击关闭时的对话框结果:

DialogResult = DialogResult.OK;

然后显示对话框时,先查看对话框结果:

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

我可以使用以下代码将数据从我的 dgv 行发送到我的文本框:

public string lat;
    public string lon;
    public string resulttt;


 private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        //Sends latitude and longitude from datagridview cell clicked to their respective textboxes//
        int rowindex = e.RowIndex;
        DataGridViewRow row = dataGridView1.Rows[rowindex];

        if (e.RowIndex < 0 || e.ColumnIndex < 0)
            return;
        lat = dataGridView1.Rows[e.RowIndex].Cells[16].Value.ToString();
        lon = dataGridView1.Rows[e.RowIndex].Cells[17].Value.ToString();
        resulttt = lat + " " + lon;
        //textBox1 = textBox1.Replace(",", ".").Replace(" ", ",");

    }

您可以根据您的程序更改值,也可以更改文本框名称。如果对你有用,现在就让我来吧。