将数据绑定到数据网格组合框 [英] bind data to data grid combobox

查看:95
本文介绍了将数据绑定到数据网格组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有datagrid视图,并且在它的datagridcombobox列中.我想将数据绑定到该datagrid组合框列

解决方案

Soft009写道:

我有datagrid视图,并且在它的datagridcombobox列中.



我真的没有得到这部分

Soft009写道:

我想将数据绑定到该datagrid组合框列



但是我在这里确实了解了一点,你的意思是你想把数据绑定到组合框上,然后她就可以给你一些东西

说明:如果我从组合框中选择一个特定字段,那么这段代码中的内容就是一个组合框和gridview,那么与该特定字段相关的所有数据都将被提取并显示在datagrid

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //use this namespace for data connectivity with sql

namespace Record_Holder
{
    public partial class InquiryDetails : Form
    {
        public InquiryDetails()
        {
            InitializeComponent();
        }
//on page load data will be filled in the combobox
        private void InquiryDetails_Load(object sender, EventArgs e)
        {

            dataGridView1.Visible = false;
//create the object of sqlconnection class 
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            conn.Open();
            string command = "select distinct time_of_inquiry from Inquiry_Table";
            SqlCommand cmd = new SqlCommand(command, conn);
            
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
//adding data to combobox
                comboBox1.Items.Add(dr[0].ToString());
            }
            conn.Close();
//close the connection 
        }
//you have a selected index change event i have wrote the code in this event so that whenever any value is selected based upon that value i can filter up my records

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x=0;
            SqlConnection connect = new SqlConnection();
            connect.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            connect.Open();
            string mycommand = "select * from Inquiry_Table where time_of_inquiry=''" + comboBox1.Text + "''";
            SqlCommand cmd = new SqlCommand(mycommand, connect);
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataTable dt;
            adp.Fill(ds);
            dt = ds.Tables[0];
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                dataGridView1.Visible = true;
                dataGridView1.DataSource = dt;
                x = x + 1;              
            }
//also i have shown a messagebox to display total record found
            MessageBox.Show("Total Inquiry found " + x.ToString(),"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            connect.Close();

        }

        
    }
}



一旦找到有用的答案,就对我的答案进行评分

谢谢&问候
基数:rose:


您可以通过编程和DataSource,DisplayMemeber等.


hi I have datagrid view and in it datagridcombobox column. I want to bind data to that datagrid combo box column

I have datagrid view and in it datagridcombobox column.



i really didnt get this part

I want to bind data to that datagrid combo box column



but i did understand a bit here, u mean u wana bind data to the combobox right well then her''s something for ya

explanation: what i have in this code is a combobox and gridview if i select a particular field from my combobox then all the data related to that particular field will be fetched up and will be shown in the datagrid

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //use this namespace for data connectivity with sql

namespace Record_Holder
{
    public partial class InquiryDetails : Form
    {
        public InquiryDetails()
        {
            InitializeComponent();
        }
//on page load data will be filled in the combobox
        private void InquiryDetails_Load(object sender, EventArgs e)
        {

            dataGridView1.Visible = false;
//create the object of sqlconnection class 
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            conn.Open();
            string command = "select distinct time_of_inquiry from Inquiry_Table";
            SqlCommand cmd = new SqlCommand(command, conn);
            
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
//adding data to combobox
                comboBox1.Items.Add(dr[0].ToString());
            }
            conn.Close();
//close the connection 
        }
//you have a selected index change event i have wrote the code in this event so that whenever any value is selected based upon that value i can filter up my records

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x=0;
            SqlConnection connect = new SqlConnection();
            connect.ConnectionString = "Data Source=.;Initial Catalog=RJIT;Integrated Security=True";
            connect.Open();
            string mycommand = "select * from Inquiry_Table where time_of_inquiry=''" + comboBox1.Text + "''";
            SqlCommand cmd = new SqlCommand(mycommand, connect);
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataTable dt;
            adp.Fill(ds);
            dt = ds.Tables[0];
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                dataGridView1.Visible = true;
                dataGridView1.DataSource = dt;
                x = x + 1;              
            }
//also i have shown a messagebox to display total record found
            MessageBox.Show("Total Inquiry found " + x.ToString(),"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            connect.Close();

        }

        
    }
}



Do rate my answer once you find it useful

Thanks & Regards
Radix :rose:


You can do that programatically and this[^] page on MSDN shows how to do that.

Or you can do it in the designer. Select your DataGridView and then the Smart tag (little arrow at top right) from the smart menu select ''Edit Columns''. When the dialog opens select your DataGridViewComboBoxColumn and you will find the DataSource, DisplayMemeber etc. in the property grid.


这篇关于将数据绑定到数据网格组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆