Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I really don't know why it gives at form loading event.
Because I could use this codeline "gridView1.GetDataRow(i)["Shortcut"].ToString()" gridView's KeyDown event without any exceptions.
C#
private void frmShrtct_Load(object sender, EventArgs e)
        {
            this.dt2.Fill(this.ds.SHRT_CUT);
            this.dt.Fill(this.ds.DB_SHORTCUT);
            gridControl1.DataSource = bs;
            SqlCommand forOku = new SqlCommand("SELECT Shortcut FROM DB_SHORTCUT",baglanti);
            baglanti.Open();
           SqlDataReader oku= forOku.ExecuteReader();
           ArrayList names = new ArrayList();
           while (oku.Read())
           {
               names.Add(oku["Shortcut"]);
           }
           forOku.Dispose();
           oku.Dispose();     
           for (int i = 0; i <names.Count ; i++)
           {
              if (names[i] == gridView1.GetDataRow(i)["Shortcut"].ToString()) //this line
                { }
                else
                {
                    gridView1.GetDataRow(i)["Shortcut"] = names[i];
                }
           }
        }
Posted
Updated 17-Jul-14 5:32am
v3
Comments
[no name] 17-Jul-14 11:40am    
Well debug your code and find out. You will find that something in your grid is null. Since it's a form load event, your grid probably does not have any rows in it yet.
Cenkay Vergili 17-Jul-14 12:40pm    
You are probably right (:
Cenkay Vergili 17-Jul-14 12:43pm    
Thank you all. Since I started to coding this site helped me so much.
With articles and answers. Thank you all <3

1 solution

Simple: setting the DataSource does not immediately load the Grid with data - it signals a DataSourceChanged event[^] to the Grid which causes it to load itself. Since you are inside an event handler, the event is queued for after the current handler finishes.

Until then, you can't use the data in the Grid, because it isn't loaded yet.

Move your code to use it into the Form.Shown event instead, and it should work.
 
Share this answer
 
Comments
Cenkay Vergili 17-Jul-14 12:42pm    
Thank you so much just saved my day.
OriginalGriff 17-Jul-14 13:57pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900