0

How can I bind the button text on form dynamically?

I want to give the user a feature where he can change the button text dynamically from the database.

So, from database, I am getting value as a dataset <code>Dataset</code> where I am getting value

Now the problem is, how can I bind this text to my button control on my form.

Can you please help me with this.

Thanks in advance.

3
  • Where is your button? What code have you tried? Commented Apr 8, 2014 at 8:20
  • You wanna change the Name property of some button to whatever user types? Commented Apr 8, 2014 at 8:24
  • I want to display the text of that is coming form db Like bttnSpokeToPeson will display the correcsponding value that is coming event text of that `Spoke to Person and they will call Back' Commented Apr 8, 2014 at 8:59

1 Answer 1

1

First convert your Dataset to DataTable and use the below code . I have tested this code according to your question.

DataTable _ds = _commonDAC.GetButtonName(1); // i assume you are getting dataset form here 


            foreach (DataRow row in _ds.Rows)
            {
                string ControlName = row["ControlName"].ToString();
                if( ControlName == "bttnLeftMessageOnMachine")
                     bttnLeftMessageOnMachine.Text = row["EventText"].ToString();
                if (ControlName == "bttnSpokeToPerson")
                    bttnSpokeToPerson.Text = row["EventText"].ToString();
                if (ControlName == "bttnHomeKitchenNotReady")
                    bttnHomeKitchenNotReady.Text = row["EventText"].ToString();
                if (ControlName == "bttnInstallerNotReady")
                    bttnInstallerNotReady.Text = row["EventText"].ToString();
                if (ControlName == "bttnNoAnswer")
                    bttnNoAnswer.Text = row["EventText"].ToString();
                if (ControlName == "bttnBusyPhoneLine")
                    bttnBusyPhoneLine.Text = row["EventText"].ToString();
                if (ControlName == "bttnPhLineNotOperation")
                    bttnPhLineNotOperation.Text = row["EventText"].ToString();
                if (ControlName == "bttnCustomerWillCallInstaller")
                    bttnCustomerWillCallInstaller.Text = row["EventText"].ToString();
                if (ControlName == "bttnIncorrectPhNo")
                    bttnIncorrectPhNo.Text = row["EventText"].ToString();
                if (ControlName == "bttnOrderOnHold")
                    bttnOrderOnHold.Text = row["EventText"].ToString();
                if (ControlName == "bttnOtherNoteRequired")
                    bttnOtherNoteRequired.Text = row["EventText"].ToString();
              }
Sign up to request clarification or add additional context in comments.

2 Comments

how to convert dataset to datatable
use this code to do that i think you are getting data like this and ` DataSet _ds = db.ExecuteDataSet(cmd); DataTable _dt = _ds.Tables[0];`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.