| |

VerySource

 Forgot password?
 Register
Search
View: 2072|Reply: 16

Seeking gridview binding problem

[Copy link]

1

Threads

11

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Latvia

Post time: 2020-11-26 22:00:01
| Show all posts |Read mode
I have two tables
Table A
audioID audioName UserId audioRemark

Table B

ID CommTxt(message) audioID


I want to use gridview to display all audio information of a user through UserId

Display like this

audioName audioRemark countcomm (count the number of stays)


How to do this, do you use gridview nesting

Take a look, what I did below, there is a problem with this, thank you for your advice! ! !


public int commcount; bind this statistical field in html,

private void MyCreateBind()
    {

        int uid = Int32.Parse(Session["UserId"].ToString());

        string str = "select * from UsersAudioView where UserId=" + uid;

        SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings["mysql"].ConnectionString);
        myCon.Open();

        SqlDataAdapter sda = new SqlDataAdapter(str, myCon);
        DataSet ds = new DataSet();
        sda.Fill(ds, "UsersAudioView ");
        AudioInfoGV.DataSource = ds.Tables["UsersAudioView "];
        AudioInfoGV.DataBind();
        myCon.Close();
    }


   protected void audioInfoGV_RowDataBound(object sender, GridViewRowEventArgs e)
    {



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


            
           // DataList dali = (DataList)e.Row.FindControl("DataList1");
            
            Label lblvid = (Label)e.Row.FindControl("lblaudioId");
   


            string aid = lblvid.Text.ToString();

            
            string str = "select count(*)as aa from table B where audioId='" + aid+ "'";


            SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings["mysql"].ConnectionString);
            myCon.Open();

            SqlCommand myCmd = new SqlCommand(str, myCon);

           // SqlDataAdapter sda = new SqlDataAdapter(str, myCon);
          // DataSet ds = new DataSet();
           // sda.Fill(ds);
            SqlDataReader dr = myCmd.ExecuteReader();


            if (dr.Read())
            {

                commcount = Int32.Parse(dr["aa"].ToString());


            }
            else
            {

                commcount = 0;


            }
           // dr.Close();
           // dali.DataSource = ds;
           // dali.DataBind();
           dr.close();

        }
}
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-11-27 12:30:01
| Show all posts
Is commcount necessary?
After finding the radio according to the userID, write it in the front desk, right?
<asp:GridView ID="audioInfoGV" runat="server" AutoGenerateColumns="false" OnRowCommand="audioInfoGV_RowCommand" OnRowDataBound="audioInfoGV_RowDataBound">
    <Columns>
         <asp:BoundField DataField="audioName" HeaderText="audio Name"/>
         <asp:BoundField DataField="audioRemark "HeaderText="audio Remark"/>
    <Columns>
<asp:GridView>
See if it can meet your requirements
Reply

Use magic Report

1

Threads

11

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-11-27 13:30:02
| Show all posts
terrysx

I want to count the number of messages left by a certain audio of this user,
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-11-27 23:00:01
| Show all posts
At the front desk: <%=commcount%> is it all right?
Reply

Use magic Report

1

Threads

11

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-11-28 10:45:01
| Show all posts
<%=commcount%> Isn't it enough?

I used it, no, just here
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-11-28 11:00:01
| Show all posts
It gives me the feeling that your program will run very slowly in audioInfoGV_RowDataBound. I don't think you need to query table B once for each row, which is too slow. Suggestion: I don’t know the field you want to display, so I deal with it step by step.
DataSet ds = new DataSet();
string strA = "select * from A where UserId=" + uid;
.......
sda.Fill(ds, "A");

string strB = "select A.audioId,count(B.*) as aa from A inner join B on A.audioId = b.audioId where A.UserId = "+ uid +" Group by A.audioId"
sda.fill(ds, "B");

In this way, the A table and the B table have a one-to-one relationship. The number of messages in the A table is in the aa field of the B table, and the connected field is audioId. (If a certain audioId in table A does not record the connection in table B, then its message record is 0.)
Reply

Use magic Report

1

Threads

11

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-11-28 11:15:01
| Show all posts
miosoul

Thank you buddy, hehe, you have a good idea, although I haven't done it yet,
FK U!
Reply

Use magic Report

1

Threads

11

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-11-28 11:45:01
| Show all posts
miosoul


The aa field, how to bind it to the gridview, I bind it, and it does not display
Reply

Use magic Report

1

Threads

11

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-11-28 12:00:01
| Show all posts
miosoul


I use
ds.Relations.Add();
VideoInfoGV.DataSource = ds.Tables["A"].DefaultView;
VideoInfoGV.DataBind();


"0" is not displayed if there is no message, only the specific number can be displayed if there is a message
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-11-28 12:15:01
| Show all posts
Then you can create a commcount field in table A, the type is typeof(int), ds.Table["A"].Colums["commcount"].Default = 0;
ds.Table["B"].PrimaryKey = new new System.Data.DataColumn[]{ds.Table["B"].Colums["audioId"]};
foreach (DataRow myRow in ds.Table["A"].Rows)
{
    DataRow drB = ds.Table["B"].Find(myRow["audioId"].ToString());
    if (drB != null)
        myRow["commcount"] = drB["aa"];
    else
        myRow["commcount"] = 0;
}

After adding such a paragraph, directly bind Table A to the DataGrid.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list