Hi,I would like to do a sorting when i had add new record,the new record must shown on the top of datagridview.It will always show latest record on top of the others old records.I had try several method that found from website but still cannot work!!!
code for load database to datagridview

 using (TMPB_attn_DAL dalObj = new TMPB_attn_DAL())
                {
                    dataGridView1.DataSource = dalObj.GetRecords().Cast<TMPB_attn>().ToList();
                    dataGridView1.Refresh();  
                }

code for update new record

private void txt_proximity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                TMPB_attn row_TMPN_attn = BusinessLogicLayer.TMS_GET_PROXIMITY_VALID_TO_USE(txt_proximity.Text);
                if (row_TMPN_attn == null)
                {
                    if (!String.IsNullOrEmpty(txt_proximity.Text))
                    {
                        using (TMPB_attn_DAL dalObj = new TMPB_attn_DAL())
                        {
                            try
                            {
                                if ((!(txt_ic.Text == string.Empty)))
                                {
                                    if ((!(txt_name.Text == string.Empty)))
                                    {
                                            TMPB_attn row = new TMPB_attn();
                                            row.attn_empName = txt_name.Text;
                                            row.attn_empIC = txt_ic.Text;
                                            row.attn_proximityNo = txt_proximity.Text;
                                            row.attn_dateTimeIn = Convert.ToDateTime(txt_datetime.Text);
                                            row.gen_modifyUser = SYSTEM_USER_LOGIN.UserName;
                                            row.gen_entryUser = SYSTEM_USER_LOGIN.UserName;
                                            row.gen_actStatus = "A";
                                            row.attn_category = "T";
                                            dalObj.UpdateRecord(row);
                                            MessageBox.Show("Record Added");
                                            txt_proximity.Text = "";
                                            txt_ic.Text = "";
                                            txt_name.Text = "";
                                            txt_datetime.Text = "";
                                            txt_proximity.Enabled = false;
                                            loadGridView();
                                    }
                                    else
                                    {
                                        MessageBox.Show("Please try again");
                                        txt_proximity.Text = "";
                                        txt_ic.Text = "";
                                        txt_name.Text = "";
                                        txt_datetime.Text = "";
                                        txt_proximity.Enabled = false;
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("Please try again");
                                    txt_proximity.Text = "";
                                    txt_ic.Text = "";
                                    txt_name.Text = "";
                                    txt_datetime.Text = "";
                                    txt_proximity.Enabled = false;
                                }
                            }
                            catch (Exception es)
                            {
                                MessageBox.Show(es.Message);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please try again");
                        txt_proximity.Text = "";
                        txt_ic.Text = "";
                        txt_name.Text = "";
                        txt_datetime.Text = "";
                        txt_proximity.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show("Proximity No. is exist");
                    txt_proximity.Text = "";
                }
            }
        }

here are the code that had been put in and try before but nothing happen

dataGridView1.Sort(dataGridView1.Columns["attn_id"], ListSortDirection.Descending);

Hi

I did a basic test with a DataTable as the data source for the Grid and your sort code works fine:

int gridCounter;
DataTable gridData;

private void button1_Click(object sender, EventArgs e)
{
    if (gridData == null)
    {
        gridData = new DataTable();
        gridData.Columns.Add("attn_id", typeof(int));
        gridData.Columns.Add("StringValue", typeof(string));
    }

    gridCounter += 1;

    DataRow row = gridData.NewRow();
    row["attn_id"] = gridCounter;
    row["StringValue"] = "String " + gridCounter.ToString();
    gridData.Rows.Add(row);

    dataGridView1.DataSource = gridData;

    dataGridView1.Sort(dataGridView1.Columns["attn_id"], ListSortDirection.Descending);
}

This leads me to ask what type of data is the DataGridView bound to as I see you have some form of DAL which you are converting to a List?

Also, does the DataGridView itself have to be sorted or can you sort it's underlying data source?

HTH

Hi,I not really get your meaning of "what type of data is the DataGridView bound".But i can said that my datagridview will show out the record that save in database by using TMPB_attn.TMPB_attn is use to connect the database.
And datagridview does not have any button for sorting.I just want it sort by itself maybe...But i still don't know how to solve this problem.

I not really get your meaning of "what type of data is the DataGridView bound"

What I mean is, what does this return dalObj.GetRecords().Cast<TMPB_attn>().ToList()? Also, can this list be sorted prior to binding to the DataGridView

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.