简体   繁体   中英

Creating column Combobox in Datagridview

I am new to C# . I want to create combobox in first column of my Datagridview . Following is the routine I have wrote. But it is adding combo in last column after setting up my grid.

For setting up Grid, i have tried the below code:

private void SetGrid()
{
    dgDetail.AutoGenerateColumns = false;
    dgDetail.ColumnCount = 5;
    dgDetail.Columns[0].Name = "Debit";
    dgDetail.Columns[0].HeaderText = "Debit Account Name";
    dgDetail.Columns[1].Name = "Bank";
    dgDetail.Columns[1].HeaderText = "Bank";
    dgDetail.Columns[2].Name = "ChqNo";
    dgDetail.Columns[2].HeaderText = "CC/Chq No";
    dgDetail.Columns[3].Name = "ChqDate";
    dgDetail.Columns[3].HeaderText = "Chq Date";
    dgDetail.Columns[4].Name = "Amount";
    dgDetail.Columns[4].HeaderText = "Amount";
    dgDetail.AllowUserToDeleteRows = true;
    dgDetail.Columns[0].Width = 280;
    dgDetail.Columns[1].Width = 160;
    dgDetail.Columns[2].Width = 90;
    dgDetail.Columns[3].Width = 90;
    dgDetail.Columns[4].Width = 120;
    dgDetail.RowsDefaultCellStyle.ForeColor = Color.Black;
    dgDetail.RowsDefaultCellStyle.BackColor = Color.White;
    dgDetail.Font = new Font("Arial", 9, FontStyle.Regular);
}

For Creating the combobox which is filled from DB.

private void FillGridCombo()
{
    SqlConnection sqlConnection = new SqlConnection(strCon);
    sqlConnection.Open();
    try
    {
        string selectQueryStringMonth = "SELECT accode, GLAC FROM glmast where (actype = 'CSH' and titleac <> 'PDP') OR TITLEAC = 'DIS' ORDER BY GLAC";

        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
        SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

        DataTable dataTable = new DataTable();
        sqlDataAdapter.Fill(dataTable);
        BindingSource bindingSourceMonth = new BindingSource();
        bindingSource.DataSource = dataTable;

        //Adding  Combo
        DataGridViewComboBoxColumn ColumnAcc = new DataGridViewComboBoxColumn();
        ColumnAcc.DataPropertyName = "Debit Account Name";
        ColumnAcc.HeaderText = "Debit Account Name";
        ColumnAcc.Width = 280;

        ColumnAcc.DataSource = bindingSourceMonth;
        ColumnAcc.ValueMember = "accode";
        ColumnAcc.DisplayMember = "GLAC";

        dgDetail.Columns.Add(ColumnAcc);
        dgDetail.DataSource = bindingSource;
    }

    catch (Exception ex)
    {
        MessageBox.Show("Error : " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    finally
    {
        if (sqlConnection.State != ConnectionState.Closed)
        sqlConnection.Close();
    }
}

I am calling both procedures on my NewData() like this.

private void NewData()
{
    if (dgDetail.DataSource != null)
        dgDetail.DataSource = null;
    else
        dgDetail.Rows.Clear();

    ClearData();
    CtrlEnable();
    SetGrid();
    FillGridCombo();
}

Help / Guide me to achieve this.,

use

dgDetail.Columns.Insert(0, ColumnAcc);

instead of

dgDetail.Columns.Add(ColumnAcc);

when you use Add it simply adds it as the last column, while using Insert you can choose where to add it to.

public virtual void Insert( int columnIndex, DataGridViewColumn dataGridViewColumn )

note that the columnIndex is a zero-based index so 0 is the first column

您可以这样使用Insert方法而不是Add

dgDetail.Columns.Insert(0,ColumnAcc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM