Hi all i want to add my Column names in a combo box for some purpose But how i will do that because i dont know the number of columns in the table and new column in the table can be added from the application. So the column size is unknown. Any idea plz

Dani AI

Generated

— since columns can be added at runtime, treat the schema as data: query the database for column names when you populate the combo box rather than hard-coding or relying only on a client-side list. 's client list is useful when the app is the sole source of changes; 's UI property approach works for grid-driven views. If other apps or DB users can change the table, read the DB metadata so the combo always reflects the real schema.

For MySQL the simplest, reliable approach is to read INFORMATION_SCHEMA (or use SHOW COLUMNS / DESCRIBE) and order by ORDINAL_POSITION to preserve column order. Example query:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'your_database'
  AND TABLE_NAME = 'your_table'
ORDER BY ORDINAL_POSITION;

MySQL docs: and SHOW COLUMNS.

If you build the UI from the data layer instead, most drivers expose schema metadata (ADO.NET GetSchema / GetSchemaTable, JDBC ResultSetMetaData, etc.), which avoids SQL parsing and works across DBs. See ADO.NET schema docs for examples: .

Notes and cautions: refresh the combo after any schema change (or when the dropdown opens) to avoid stale lists; ensure the DB user has rights to read metadata; and be careful about letting the app alter schema at runtime — it complicates migrations, backups, and client compatibility. If you need truly dynamic attributes, consider a metadata table, an EAV/key-value model, or a JSON column (MySQL supports JSON) instead of frequently adding actual table columns. See MySQL JSON docs: JSON Data Type.

Recommended Answers

All 2 Replies

Keep an arraylist or some other sort of list of columns.

ArrayList columns = new ArrayList();
columns.Add("column1");

int columnCount = columns.Count;

you can just use

int count =dataGridView1.Columns.Count;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.