Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello brothers


i want to Fill textbox inside a DataGridView with data form database depended on selected value from combobox inside the DataGridView

this is the code for filling the cobobox
C#
private void create_report_Load(object sender, EventArgs e)
        {
            SqlConnection objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
            SqlCommand objCmd = new SqlCommand("SELECT * FROM [testing_name];", objCon);
            SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
            objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
            DataTable dt = new DataTable();

            objDA.Fill(dt);
            Test_Name.DataSource = dt;

            Test_Name.DisplayMember = "test_name";

            Test_Name.ValueMember = "test_defualt_id";       

        }



and i try this code for filling the textbox

C#
private void Test_Name_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection cnz = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
            SqlCommand cmdzs = new SqlCommand("SELECT * FROM [test_normal] WHERE test_id='" + Test_Name.Selected + "' ;", cnz);
            cmdzs.CommandType = CommandType.Text;
            cmdzs.Connection.Open();
            SqlDataReader dr = cmdzs.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    //test_result.Text = dr["patient_gender"].ToString();
                    //reference_rang.Text = dr["patient_birth_date"].ToString();
                }
            }
            cmdzs.Connection.Close();
        }



but give me error with no property to the textbox that inside the DataGridView

this image can explain more

http://img545.imageshack.us/img545/3850/d884.png[^]








#new_edit


i try this code

C#
public create_report()
        {
            InitializeComponent();
            define_gridview_columns();
            add_rows();
        }


        public void define_gridview_columns()
        {
           

            DataGridViewComboBoxColumn test_name = new DataGridViewComboBoxColumn();
            test_name.HeaderText = "Test Name";
            test_name.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            SqlConnection objCon = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
            SqlCommand objCmd = new SqlCommand("SELECT dbo.testing_name.test_name,dbo.testing_name.test_defualt_id FROM dbo.testing_name ;", objCon);
            SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
            objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
            DataTable dt = new DataTable();
            objCon.Open();
            objDA.Fill(dt);
            test_name.DataSource = dt;
            test_name.DisplayMember = "test_name";
            test_name.ValueMember = "test_defualt_id";
            objCon.Close();


            DataGridViewTextBoxColumn test_unit = new DataGridViewTextBoxColumn();
            test_unit.HeaderText = "Normail Unit";
            test_unit.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


            DataGridViewTextBoxColumn ref_rang = new DataGridViewTextBoxColumn();
            ref_rang.HeaderText = "Reference Rang";
            ref_rang.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            

            DataGridViewTextBoxColumn new_result = new DataGridViewTextBoxColumn();
            new_result.HeaderText = "New Result";
            new_result.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

           


            dataGridView1.Columns.Add(test_name);
            dataGridView1.Columns.Add(test_unit);
            dataGridView1.Columns.Add(ref_rang);
            dataGridView1.Columns.Add(new_result);
        }

        public void add_rows()
        {

            for (int i = 0; i < 10; i++)
            {
                dataGridView1.Rows.Add();
            }

        }


this create the controll that i need

and i fill the cobobox with the data that i need

now what i need is get data from the DB to the created textbox
i use this code but the property not existed for the datagridview textbos

C#
//SqlConnection cnz = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
           //SqlCommand cmdzs = new SqlCommand("SELECT dbo.test_normal.test_normail_unit,dbo.test_normal.test_normal_reference_rang_id FROM dbo.test_normal WHERE dbo.test_normal.test_normal_id='" + test_name.ValueMember + "' ;", cnz);
           //cmdzs.CommandType = CommandType.Text;
           //cmdzs.Connection.Open();
           //SqlDataReader dr = cmdzs.ExecuteReader();
           //if (dr.HasRows)
           //{
           //    while (dr.Read())
           //    {

           //        test_unit.Text = dr["patient_gender"].ToString();
           //        ref_rang.Text = dr["patient_birth_date"].ToString();

           //    }
           //}
           //cmdzs.Connection.Close();
Posted
Updated 13-Nov-13 12:24pm
v2

Hello ,

you can use DatagridView EditingControlShowing event to achieve this .

C#
private void dataGridViewSales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           ComboBox cb = e.Control as ComboBox;
           if (cb != null)
           {
               cb.SelectedIndexChanged += new EventHandler(selectionchange);
              //here 'SelectedIndexChanged'  event is used
           }
       }


and then write 'selectionchange' method
C#
void selectionchange(object sender, EventArgs e)
       {
           try
           {
               ComboBox cb = (ComboBox)sender;
         //write here the code to get the value according to combobox selection change
           }
           catch { }
       }


thanks
animesh
 
Share this answer
 
Comments
Abdullaziz Said 13-Nov-13 18:19pm    
hello

i am sorry i can't understand this code or what this suppose to do


but the image explain more


i need to add inside the datagridview
1 combox 3 textboxs 2 of them i want to fill them with data depended on the combox selected value and the the textbox for insert new data to table in database


i hope i make good explain

sorry for english
Animesh Datta 14-Nov-13 0:59am    
datagridview has many events like 'CellValueChanged' , 'cellendedit' , 'EditingControlShowing' etc . i suggest you to use 'EditingControlShowing' event to achieve your requirement .

Unfortunately the DataGridViewComboBoxColumn doesn't expose a SelectedIndexChanged Event.
In order to catch an index change of a ComboBoxColumn, you 'll first need to cast the cell to a DataGridViewComboBoxEditingControl and Add an EventHandler to that control. This you will have to do in the EditingControlShowing Event of the DataGridView

DataGridView.EditingControlShowing Event Occurs when a control for editing a cell is showing. here you want to change the data depend upon the selected value of combobox . but first you have to recognize the control (here combobox) which fires the event . thats why i write this code

ComboBox cb = e.Control as ComboBox;


and then fire it by calling its SelectedIndexChanged event by this code

cb.SelectedIndexChanged += new EventHandler(selectionchange);


and finally define the method selectionchange

try this link also
http://stackoverflow.com/questions/11950189/how-to-handle-selectedindexchanged-event-for-a-combobox
i think you don't need that much of work there,
just use the cellendedit event or CellValueChanged[^] Event

for further details see the example given in cellvaluechanged event
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900