SelectionChanged上的C#Master Detail gridview

时间:2016-12-01 12:18:51

标签: c#

我有一个带有grid(dataGridView1)和textbox(txtSearch)的表单。当我通过字段acSubject在文本框网格过滤器中键入内容时。现在我放入第二个网格,我想要新的自定义SQL查询,这将取决于dataGridView1中的选定行。 SQL将是: ''''从the_setsubjcontact中选择anUserID,其中acSubject = @acSubject''''

我怎么能这样。 编码:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=local\s08r2;Initial Catalog=Demo;User id=sa;Password=sa";
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter(@"
            SELECT acSubject, acAddress, acPost, acName, acPhone, 
            acFieldSA, acFieldSB, acFieldSC, acFieldSD, acFieldSE, 
            anFieldNA, anFieldNB, anFieldNC, anFieldND, anFieldNE, OdgovornaOsoba, acSubjTypeBuyer
            FROM ARS.dbo._ARSCRM_vSubjekti
            ", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;

        }
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void txtSearch_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtSearch.Text))
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;
        }
        else
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("acSubject like '%{0}%'", txtSearch.Text);
        }
    }

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        DataGridView dgv = (DataGridView)sender;

        //User selected a cell (show the first cell in the row)
        if (dgv.SelectedCells.Count > 0)
            txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[5].Value.ToString();

    }
}

当我使用C#中的DataSet时,我获得了成功,但是使用CustomSQL我不知道如何做到这一点。请帮忙。

1 个答案:

答案 0 :(得分:1)

private void ShowDetails(int UserId)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=local\s08r2;Initial Catalog=Demo;User id=sa;Password=sa";
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter(@"
           select anUserID from the_setsubjcontact where acSubject = @acSubjec", con);
da.SelectCommand.Parameters.AddWithValue(@acSubjec, UserId.ToString());
        DataTable dt = new DataTable();
        sda.Fill(dt);
        dataGridView2.DataSource = dt;

        }

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        DataGridView dgv = (DataGridView)sender;

        //User selected a cell (show the first cell in the row)
        if (dgv.SelectedCells.Count > 0 && dgv.SelectedCells[0].RowIndex >-1 &&  dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells.Count > 0)
            txtAcFieldSA.Text = dgv.Rows[dgv.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
ShowDetails(int.Parse(txtAcFieldSA.Text));

    }