2

I have one datagrid and I want to show three column using it. I have used stored procedure to show some records into datagrid.

stored procedure is :

procedure [dbo].[allKeys]
AS 
select id,key,username from keys_TB

the key in this procedure is encrypted so, need to convert it i have done that conversion and have keep those key is ArrayList.

But problem is have bind the remaining id,username columns to datagrid, unable to bind ArrayList to datagrid.

Code behind :

DataSet ds = clsBusinessLogic.allKeys();
dgrv_allkey.DataSource = ds.Tables[0];
dgrv_allkey.AutoGenerateColumns = false;
dgrv_allkey.Columns["id"].DataPropertyName = "id";
DataGridViewColumn id = dgrv_allkey.Columns[0];
this.dgrv_allkey.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
id.Width = 60;

dgrv_allkey.Columns["username"].DataPropertyName = "username";
DataGridViewColumn Custname = dgrv_allkey.Columns[2];
Custname.Width = 199;
this.dgrv_allkey.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

this is how i have bind id and username to datagrid but don't have any clue about key.

and i have called the stored procedure [allKeys] through clsBusinessLogic.allKeys() method

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Chetan Bodke
  • 523
  • 6
  • 28

1 Answers1

0

First add an databinding complete event to the grid:

dgrv_allkey.DataBindingComplete += dgrv_allkey_DataBindingComplete;

Then add the result set of stored procedure as the grid datasoruce:

dgrv_allkey.DataSource = ds.Tables[0];
dgrv_allkey.Columns["id"].DataPropertyName = "id";
////and other formating what ever needed. 

Due to setting the datasource the event will be fired, and then in this event change the value of the key cell for each row.

private void dgrv_allkey_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    // Change Column Value for the DataGridView. 
    foreach (DataGridViewColumn i in dataGridView1.Columns)
    {
        if (i.Name == "key")
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string oldvalue = row.Cells["key"].Value as string;

                if (!string.IsNullOrEmpty(oldvalue))
                {
                    // perform decoding here and set the decoded value here..
                }
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dreamweaver
  • 1,328
  • 11
  • 21