我创建了一个gridview来显示多个数据?但它不起作用? [英] i have created a gridview to show multiple data? but it's not working?

查看:82
本文介绍了我创建了一个gridview来显示多个数据?但它不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   asp:GridView     ID   =  GridView1     runat   =  server    AutoGenerateColumns   =  False    DataSource   =  sqldread2 >  <   / asp:GridView  >  







  String  selQuery =   SELECT Id FROM MapDataImage WHERE Source =' + TextBox1 .Text +  ';; 
{

SqlCommand scmd = new SqlCommand(selQuery,con);

con.Open();

SqlDataReader sqldread = scmd.ExecuteReader();

sqldread.Read();
Dbid =( int )sqldread [ ID];
Label4.Text = Convert.ToString(Dbid);
sqldread.Dispose();
if (Dbid!= null
{
String QueryStr = SELECT * FROM User_Images WHERE Id =' + Dbid + ';
// String QueryStr =SELECT * FROM User_Images GROUP BY Id HAVING(COUNT(*)> 1 );
scmd1 = new SqlCommand(QueryStr,con);
scmd1.CommandType = CommandType.Text;
scmd1.Connection = con;

SqlDataReader sqldread2 = scmd1.ExecuteReader();
while (sqldread2.Read())
{
Context.Response.BinaryWrite(( byte [])sqldread2 [ Image]);
Context.Response.ContentType = image / jpg;

al.Add(sqldread2);
sDatAdp.SelectCommand = scmd1;

GridView1.DataSource = sqldread2;
GridView1.DataBind();
}
sqldread2.Dispose();
}





这里我将这些图片传递给gridview,但我怎么能查看那些........ ....

我想要选择与ID相关的所有项目。

所以这就是我的sql查询没问题?

请有人检查天气是不是错了?



例子



情景



假设我有一个数据库1



[id] [name]

1 john

2 ann



如果我的文本框值等于john ....它会从中得到id。

所以id为1



然后它将从数据库2中选择与该特定Id相关的所有图像



数据库2



[id] [图片]

1图片1

2图片2

1图片3

1图片4

.............



所以输出将是



[id] [图片]

1张图片1

1张图片3

1张图片4



请有人帮助我.....我试过这么多次?

解决方案

在网格内创建一个模板字段,然后在模板字段中放置另一个模板字段然后通过在rowdatabound事件中找到它来绑定它(即)

< asp:GridView ID =   GridView1  OnRowDataBound =   GridView1_RowDataBound runat =   server AutoGenerateColumns = < span class =code-string>  False DataSource =   sqldread2 >  
< Columns>
< asp:TemplateField visible = False >
< ItemTemplate>< asp:label ID = lblID runate = 服务器 Text = ' <%#Eval(Id)%>' visible = < span class =code-string> False /> < / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = 删除 >
< ItemTemplate> ;
< asp:GridView ID = InnerGrid runat = 服务器 AutoGenerateColumns = false >
< Columns>
< asp:TemplateField HeaderText = images >
< ItemTemplate>
< asp:Image run = server ID = Image1 ImageUrl = ' <%#Bind(SomeField)%>' width = 250px Hieght = 200px />
< / ItemTemplate >
< / asp:TemplateField >
< / >
< / asp:GridView >
< / ItemTemplate >
< / asp:TemplateField >
< Columns>
< / asp:GridView >



//代码落后

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)

{

if(e.Row.RowType == DataControlRowType.DataRow)

{

Label lblID = e.Row.FindControl(lblID )作为标签;

GridView grd = e.Row.FindControl(InnerGrid)作为GridView;

if(lblID!= null&& grd!= null )

{

String QueryStr =SELECT * FROM User_Images WHERE Id ='+ lblID.Text +';

SqlDataAdapter da = new SqlDataAdapter(QueryStr,yourconnection);

DataTable dt = new DataTable();

da.Fill(dt);

//请求到数据库并绑定innerGrid

grd.DataSource = dt;

grd.DataBind();

}





}

}


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSource="sqldread2"></asp:GridView>




String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "';";
        {

            SqlCommand scmd = new SqlCommand(selQuery, con);

            con.Open();

            SqlDataReader sqldread = scmd.ExecuteReader();

            sqldread.Read();
            Dbid = (int)sqldread["Id"];
            Label4.Text = Convert.ToString(Dbid);
            sqldread.Dispose();
                if (Dbid != null)
                {
                 String QueryStr = "SELECT * FROM User_Images WHERE Id='" + Dbid + "'";
                    //String QueryStr = "SELECT * FROM User_Images GROUP BY Id HAVING ( COUNT(*) > 1 )";
                 scmd1 = new SqlCommand(QueryStr, con);
                 scmd1.CommandType = CommandType.Text;
                 scmd1.Connection = con;
                    
                    SqlDataReader sqldread2 = scmd1.ExecuteReader();
                    while (sqldread2.Read())
                    {
                        Context.Response.BinaryWrite((byte[])sqldread2["Image"]);
                        Context.Response.ContentType = "image/jpg";

                        al.Add(sqldread2);
                        sDatAdp.SelectCommand = scmd1;

                        GridView1.DataSource = sqldread2;
                        GridView1.DataBind();
                    }
                    sqldread2.Dispose();
                }



here i pass those images to gridview, but how can i view those............
the thing is i want to select all the items which related to the ID.
so for that is it my sql query is okay?
please someone check weather it's wrong or not?

example

Scenario

let say i have a database 1

[id] [name]
1 john
2 ann

if my textbox value equals to john.... it will get id from that.
so id is 1

then it will select all the images related to that particular Id from database 2

database 2

[Id] [Image]
1 image 1
2 image 2
1 image 3
1 image 4
.............

so output will be

[Id] [Image]
1 image 1
1 image 3
1 image 4

please someone help me..... i tried to do this so many times?

解决方案

create a template field inside the grid then place another inside the template field then bind the by finding it in rowdatabound event (i-e)

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server" AutoGenerateColumns="False" DataSource="sqldread2">
<Columns>
<asp:TemplateField visible="False">
<ItemTemplate><asp:label ID="lblID" runate="Server" Text='<%#Eval("Id")%>' visible="False" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
                            <ItemTemplate>                               
                              <asp:GridView ID="InnerGrid" runat="Server" AutoGenerateColumns="false">
<Columns>
    <asp:TemplateField HeaderText="images">
<ItemTemplate>
<asp:Image run="server" ID="Image1" ImageUrl='<%#Bind("SomeField")%>' width="250px" Hieght="200px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
<Columns>
</asp:GridView>


// code behind
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblID=e.Row.FindControl("lblID") as Label;
GridView grd=e.Row.FindControl("InnerGrid") as GridView;
if(lblID!=null && grd!=null)
{
String QueryStr = "SELECT * FROM User_Images WHERE Id='" + lblID.Text+ "'";
SqlDataAdapter da=new SqlDataAdapter(QueryStr,yourconnection);
DataTable dt=new DataTable();
da.Fill(dt);
// Request to Database and bind the innerGrid
grd.DataSource=dt;
grd.DataBind();
}


}
}


这篇关于我创建了一个gridview来显示多个数据?但它不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆