Tuba
Tuba

Reputation: 75

Winform datasource update

I have a gridcontrol and I want to get data from database. I added datasource to grid but column name is not as I want. For this reason, I open the xsd datasource file and edit the column name on configure option.

enter image description here

I changed the query and I write select namesurname [NAME SURNAME] from employees

enter image description here

After this, I want to this; when I edit this gridcontrol, I push to save button and update this columns on database like tableadapter.update(..)

But my update doesn't working. I think this is because; I used to 'naming' ( [NAME SURNAME] ).

Because when I don't edit the column name like this, update is working.

Upvotes: 4

Views: 274

Answers (1)

A Farmanbar
A Farmanbar

Reputation: 4788

No need for changing column name in your database. you can define your column name in C#.

my best answer to your question is, using DataTable. there is no need to get entire table from your database in order to put it on GirdControl.

You should create a string query or StoredProcedure(sql-server) and read each returned row form your database.

first of all we create a DataTable

DataTable dt=new DataTable();

then define table columns name .

 dt.Columns.Add("name1");
 dt.Columns.Add("name2");
 dt.Columns.Add("name3");

finally start fetching data from database (StoredProcedure)

    try
    {
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("myStoredProcedure"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@param1", value1);
                    cmd.Parameters.AddWithValue("@param2", value2);
                    cmd.Parameters.AddWithValue("@param3", value3);
                    cmd.Connection = con;
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                        {
                            DataRow dr = dt.NewRow();
                            dr[0] = reader[0].ToString();
                            dr[1] = reader[1].ToString();
                            dr[2] = reader[2].ToString();           
                            dt.Rows.Add(dr);
                        }
                    return true;
                }
            }
        }
    }
    catch(Exception e)
    {

        return false;          
    }

in conclusion we have a complete Table for presenting

Upvotes: 2

Related Questions