本文介绍了如何在grideview 2表中显示第一个表的所有colomns和第二个表中的1个colomn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在单个数据gridview中显示2个表来自第一个表的所有表和来自第二个表的1个列。

i尝试但是只显示空白单元格,而不显示存储数据

帮助我,我该怎么办?



我尝试过:



i want display 2 tables in single data gridview all tables from 1st table and 1 column from 2nd table.
i tried but there show only blank cells, not show stored data
help me, what should i do?

What I have tried:

public void disp_data()
       {

            DataTable dt = new DataTable();
           {
               SqlConnection con = new SqlConnection(strcon);
               dataGridView1.DataSource = null;
               dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
               con.Open();
               SqlCommand cmd = new SqlCommand("select id,cmpny_dtl.c_name as Name,cmpny_dtl.adrs as address,cmpny_dtl.state as state,s.State as state,cmpny_dtl.c_phone as phone,cmpny_dtl.wbsit as website" + " from cmpny_dtl join State s on s.State = s.State", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(dt);
               con.Close();
               dataGridView1.ColumnCount = 9;
               dataGridView1.Columns[0].HeaderText = "id";
               dataGridView1.Columns[0].DataPropertyName = "id";
               dataGridView1.Columns[0].Visible = false;
               dataGridView1.Columns[1].HeaderText = "Name";
               dataGridView1.Columns[1].DataPropertyName = "c_name";
               dataGridView1.Columns[2].HeaderText = "address";
               dataGridView1.Columns[2].DataPropertyName = "adrs";
               dataGridView1.Columns[3].HeaderText = "GST";
               dataGridView1.Columns[3].DataPropertyName = "GST";
               //dataGridView1.Columns[3].Visible = false;
               dataGridView1.Columns[4].HeaderText = "state";
               dataGridView1.Columns[4].DataPropertyName = "State";
               //dataGridView1.Columns[4].Visible = false;
               dataGridView1.Columns[5].HeaderText = "phone";
               dataGridView1.Columns[5].DataPropertyName = "c_phone";
               dataGridView1.Columns[6].HeaderText = "website";
               dataGridView1.Columns[6].DataPropertyName = "wbsit";
               dataGridView1.Columns[7].HeaderText = "servicetax";
               dataGridView1.Columns[7].DataPropertyName = "servicetax";
               dataGridView1.Columns[8].HeaderText = "adsnldtl";
               dataGridView1.Columns[8].DataPropertyName = "adsnldtl";
               dataGridView1.DataSource = dt;
           }

       }

推荐答案

from cmpny_dtl join State s on s.State = cmpny_dtl.State


private void DisplayData()
{
    DataTable dt = new DataTable();
    {
        dataGridView1.DataSource = null;
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        con.Open();
        SqlCommand cmd = new SqlCommand("select id,p.c_name c_name,p.adrs as adrs,p.c_phone as c_phone,p.state_id as state_id,p.GST as GST,t.State as State,p.pan_no as pan_no,p.srvctax as srvctax,p.wbsit as wbsit,p.email as email,p.adsnldtl as adsnldtl,p.logo as logo from cmpny_dtl p join State t on t.State_ID = p.state_id ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        dataGridView1.ColumnCount = 14;
        dataGridView1.Columns[0].HeaderText = "ID";
        dataGridView1.Columns[0].DataPropertyName = "id";
        dataGridView1.Columns[0].Visible = false;
        dataGridView1.Columns[1].HeaderText = "Name";
        dataGridView1.Columns[1].DataPropertyName = "c_name";
        dataGridView1.Columns[2].HeaderText = "adrs";
        dataGridView1.Columns[2].DataPropertyName = "adrs";
        dataGridView1.Columns[3].HeaderText = "state_id";
        dataGridView1.Columns[3].DataPropertyName = "state_id";
        dataGridView1.Columns[3].Visible = false;
        dataGridView1.Columns[4].HeaderText = "GST";
        dataGridView1.Columns[4].DataPropertyName = "GST";
        //dataGridView1.Columns[4].Visible = false;
        dataGridView1.Columns[5].HeaderText = "State";
        dataGridView1.Columns[5].DataPropertyName = "State";
        dataGridView1.Columns[6].HeaderText = "srvctax";
        dataGridView1.Columns[6].DataPropertyName = "srvctax";
        dataGridView1.Columns[7].HeaderText = "c_phone";
        dataGridView1.Columns[7].DataPropertyName = "c_phone";
        dataGridView1.Columns[8].HeaderText = "tin_no";
        dataGridView1.Columns[8].DataPropertyName = "tin_no";
        dataGridView1.Columns[8].Visible = false;
        dataGridView1.Columns[9].HeaderText = "pan_no";
        dataGridView1.Columns[9].DataPropertyName = "pan_no";
        dataGridView1.Columns[9].Visible = false;
        dataGridView1.Columns[10].HeaderText = "wbsit";
        dataGridView1.Columns[10].DataPropertyName = "wbsit";
        dataGridView1.Columns[10].Visible = false;
        dataGridView1.Columns[11].HeaderText = "email";
        dataGridView1.Columns[11].DataPropertyName = "email";
        dataGridView1.Columns[11].Visible = false;
        dataGridView1.Columns[12].HeaderText = "adsnldtl";
        dataGridView1.Columns[12].DataPropertyName = "adsnldtl";
        dataGridView1.Columns[12].Visible = false;
        dataGridView1.Columns[13].HeaderText = "logo";
        dataGridView1.Columns[13].DataPropertyName = "logo";
        dataGridView1.Columns[13].Visible = false;
        dataGridView1.DataSource = dt;
    }


}


private void dataGridView1_CellClick(object sender,ataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentRow.Index != -1)
    {

        //id =         Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
        CmpnyName_txt.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        Adrs_txt.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        CmpnyPhone_txt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        STATE_comboBox.SelectedValue = dataGridView1.CurrentRow.Cells[5].Value.ToString();
       GST_textBox.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();

       Email_txt.Text = dataGridView1.CurrentRow.Cells[11].Value.ToString();

       Webst_txt.Text = dataGridView1.CurrentRow.Cells[10].Value.ToString();
       Pan_txt.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
        Tin_txt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        SrvcTaxNo_txt.Text = dataGridView1.CurrentRow.Cells[9].Value.ToString();
        AdsnlDtl_txt.Text = dataGridView1.CurrentRow.Cells[12].Value.ToString();
        ////AdsnlDtl_txt.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

        if (dataGridView1.CurrentRow.Cells[13].Value != DBNull.Value)
        {
            byte[] img = (byte[])dataGridView1.CurrentRow.Cells[13].Value;
            MemoryStream ms = new MemoryStream(img);
            pictureBox1.Image = Image.FromStream(ms);

            Browes_btn.Text = "Remove";
        }
        else
        {
            pictureBox1.Image = null;
            Browes_btn.Text = "Browes";
        }

        SUBMIT_btn.Text = "UPDATE";
        Display_btn.Text = "RESET";
    }
}


这篇关于如何在grideview 2表中显示第一个表的所有colomns和第二个表中的1个colomn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:58