两个GridView联动

在GridView中,如果点击“选择”,在页面中会显示另一个GridView,显示这个相应的信息?这里我用GridView2做主表,GridView2做从表。
先从GridView2的事件属性SelectedIndexChanged中写上相应的事件名:GridView2_SelectedIndexChanged;在数据属性DataKeyNames写上数据键的名字(GridView.DataKeyNames属性获取或设置一个数组,该数组包含了显示在GridView控件中的项的主键的名称,FileTransmitId)。然后编辑相应的代码:

protected   void  GridView2_SelectedIndexChanged( object  sender, EventArgs e)
{
this.ObjectDataSource3.SelectParameters[0].DefaultValue = GridView2.SelectedValue.ToString();
        ObjectDataSource3.DataBind();
        GridView3.DataBind();
        GridView3.Visible 
= true;     
}

然后在编写从数据库中获取的信息:

public   static  FileTransmit GetFileTransmits( int  fileId)
            
{
            FileTransmit fileTransmit 
= null;
            
using (SqlConnection connection = new SqlConnection(constr))
            
{
                 connection.Open();
string sql = "select * from FileTransmit  where FileTransmitId=@fileTransmitId";
                SqlCommand command 
= new SqlCommand(sql, connection);
                command.Parameters.Add(
new SqlParameter("@fileTransmitId", fileId));
                SqlDataReader reader 
= command.ExecuteReader();
                
while (reader.Read())
                
{
                   fileTransmit 
= new FileTransmit();
                   fileTransmit.FileName
=reader["FileName"].ToString();
                   fileTransmit.Content
=(byte[])reader["Attachment"];
                }

                reader.Close();
            }

            
return fileTransmit;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值