2015-05-29 34 views
2

我有一个数据网格,我想显示三列使用它。 我已经使用存储过程向datagrid显示一些记录。绑定单个数据网格与两个diff数据源(存储过程输出和ArrayList)

存储过程是:

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

在此过程中,关键是加密的,因此,需要将它转换我已经做到了转换,并有保持这些关键的ArrayList。

但问题是有剩余的id,username列绑定到datagrid,无法将ArrayList绑定到datagrid。

后面的代码:

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; 

这是我必须绑定ID和用户名的DataGrid,但没有对关键任何线索。

,我已经通过clsBusinessLogic.allKeys()方法

+0

永远不要使用'ArrayList'。它已经过时了十多年。 –

+0

@JohnSaunders请建议另一个解决方案... –

回答

0

首先添加一个数据绑定完成事件到电网调用的存储过程[allKeys]:

dgrv_allkey.DataBindingComplete += dgrv_allkey_DataBindingComplete; 

然后添加结果集的存储过程的作为电网datasoruce:

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

由于设置数据源的事件将被解雇,然后在这种情况下更改密钥c的值每行有一个椭圆。

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.. 
       } 
      } 
     } 
    } 
} 
+0

无法从该数据网格获取字符串来转换...实际上“oldvalue = row.Cells [”key“]。Value as string;”返回null ... /。不要现在为什么 –

+0

你可以检查一下数据表的列名是什么? – Dreamweaver

+0

一切正常..但是当试图从datagrid代码获取值返回null –