0

I made WinForm application with DevExpress, and it's a part of my code(C#):

public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
{
    private DataTable _Tbl;
    private DataTable CreateTable(int RowCount)
    {
        _Tbl = new DataTable();
        _Tbl.Columns.Add("New Field", typeof(string));

        for (int i = 0; i < RowCount; i++)
            _Tbl.Rows.Add(new object[] { });

        return _Tbl;
    }




    int i = 0;

    private void simpleButton1_Click_1(object sender, EventArgs e)
    {
        i++;
        int index = i % _Tbl.Columns.Count;
        DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns.AddVisible(_Tbl.Columns[index].ColumnName);
        col.Name = "Column_{0}" + i;
                }

    public XtraForm1()
    {

        InitializeComponent();
        gridControl1.DataSource = CreateTable(20);
        gridView1.Columns.Clear();

    }
}

the code will make a column at run-time when the button pressed, my question is How can I bind the selected column at XtraGrid with the data from any DBMS with the selected column from selected table in database selected at run-time

sorry for my bad English. I hope all of you can help me. Thanks

  • @Maulana Set the Datasource of your XtraGrid to your DataTable. Then give the name of the Column to the FieldName Propertie of your GridColumn. If you need Example let me know ;-) – Sebi Feb 15 '13 at 07:23
  • I think, I need an example sir. should you like to give any sample? :) thanks. – Ikhsan Maulana Feb 15 '13 at 15:08

1 Answers1

0

You need to set the name of your DataTable Column to the FieldName Propertie of your GridColumn.

If i understand you right this should help:

private void simpleButton1_Click_1(object sender, EventArgs e)
{
    i++;
    int index = i % _Tbl.Columns.Count;
    DevExpress.XtraGrid.Columns.GridColumn col = gridView1.Columns.AddVisible(_Tbl.Columns[index].ColumnName);
    col.Name = "Column_{0}" + i;
    col.FieldName = "Column_{0}" + i";
}

The FieldName Propertie tells the Grid where to find the Data. This could be the Name of a DataColumn or the name of a Propertie, if you use a List as DataSource.

Sebi
  • 3,879
  • 2
  • 35
  • 62