Populate DataGridView and make cells editable in Windows Application

Last Reply 4 months ago By PRA

Posted 4 months ago

Hi!

I used below code using DataTable load data into datagridview.

How I can write into free cells datagridview manual. It’s not editable now.

cashClasses cash = new cashClasses();
public agremForm()
{
    InitializeComponent();
    getGridAgrem();
}

public void getGridAgrem()
{
    Grid.AutoGenerateColumns = false;
    Grid.Columns[0].DataPropertyName = "Id";
    Grid.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.Columns[1].DataPropertyName = "Spec";
    Grid.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.Columns[2].DataPropertyName = "Year";
    Grid.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.Columns[3].DataPropertyName = "Money";
    Grid.Columns[3].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.Columns[4].DataPropertyName = "MNine";
    Grid.Columns[4].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.Columns[5].DataPropertyName = "Corresp";
    Grid.Columns[5].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.Columns[6].DataPropertyName = "SecEduc";
    Grid.Columns[6].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    Grid.AllowUserToAddRows = false;
    Grid.DataSource = cash.searchAgrSps();
}
public DataTable searchAgrSps()
{
    DataTable dt = new DataTable();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "select Id, Spec, '' as Year, '' as Money, '' as MNine, '' as Corresp, '' as SecEduc from special";
    con.Open();
    try
    {
       dt.Load(cmd.ExecuteReader());
    }
    catch { };
    con.Close();
    return dt;
}

 

You are viewing reply posted by: PRA 4 months ago.
Posted 4 months ago

Problem solved by using temporary table we can change datagridview cells value.

public DataTable searchAgrSps()
{
    string query = "";
    DataTable dt = new DataTable();
    SqlCommand cmd = con.CreateCommand();
    query = "select Id, Spec, '' as Year, convert(money, '')Money, convert(money,'')MNine, convert(money,'')Corresp, convert(money,'')SecEduc into result from special";
    query += " select Id, Spec, Year, Money, MNine, Corresp, SecEduc from result";
    query += " drop table result";
    cmd.CommandText = query;
    con.Open();
    try
    {
        dt.Load(cmd.ExecuteReader());
    }
    catch { };
    con.Close();
    return dt;
}