Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I use this code for read data from database sqlserver and dispaly it with Grid view

C#
string connectionworkbook = "Data Source=.;Initial Catalog=PRG_jsu;Integrated Security=True";  string select = string.Format("SELECT * FROM course,user_code Where user_code.Term >=course.showterm And user_code.username='{0}'", Session["username"].ToString());
 SqlConnection workbook = new SqlConnection(connectionworkbook);
  SqlCommand workbookcmd = new SqlCommand(select, workbook);
  SqlDataAdapter TestAdapter = new SqlDataAdapter(workbookcmd);
 DataSet workbookset = new DataSet();
  TestAdapter.Fill(workbookset, "course");
  GridView1.DataSource = workbookset;
  GridView1.DataBind();


And the code for asp.net is:


ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server">

  
     <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                    <contenttemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:Panel ID="Panel1" runat="server" ScrollBars="Auto">
    <asp:GridView ID="GridView1" Width="780px" runat="server" AutoGenerateColumns="False"  GridLines="None" Pagesize="5" Allow Paging="true" CellPadding="4" Height="24px" HeaderStyle-BackColor="#ffffcc"
   RowStyle-BackColor="#ffff99" ForeColor="#333333" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                                        OnPageIndexChanging ="GridView1_PageIndexChanging">
 <rowstyle backcolor="#b9dcff" forecolor="#333333" />
  <columns>
   <asp:BoundField DataField="Coursename" HeaderText="نام درس" />
   <asp:BoundField DataField="CourseId" HeaderText="کد درس" />
    <asp:BoundField DataField="one" HeaderText="تعداد واحد" />
   <asp:BoundField DataField="preneed" HeaderText="هم نیاز" />
    <asp:BoundField DataField="needmate" HeaderText="پیش نیاز" />
    <asp:BoundField DataField="Catenaname" HeaderText="رشته" />
     <asp:CommandField ShowSelectButton="true" />
      </columns>
 <footerstyle backcolor="#1667fe" font-bold="True" forecolor="White" />     <SelectedRowStyle BackColor="#E2DED6" ForeColor="#333333" Font-Bold="True" />  <HeaderStyle BackColor="#1667fe" Font-Bold="True" ForeColor="White" />
  <editrowstyle backcolor="#999999" />
        <alternatingrowstyle backcolor="White" forecolor="#284775" />
              
           
         </td>
        </tr>
     </table>
       </contenttemplate>


And for select that use in Grid view use this code:
C#

C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Txtname.Text = GridView1.SelectedRow.Cells[0].Text;
        Txtcode.Text = GridView1.SelectedRow.Cells[1].Text;
       
    }

my problem is this:
in the first level have Error with OnPageIndexChanging and I use this code:
protected void GridView1_PageIndexChanging(object sender, EventArgs e)
{
}
and OnPageIndexChanging="GridView1_PageIndexChanging" in asp.net but not have a good request to me please help me!!!!
Posted
Updated 29-Oct-11 1:48am
v4

1 solution

Hi,

What i understood from above is that you've enabled Paging for the gridview and wired a PageIndexChanging event and it's not firing proberly...

If my understanding is correct, then I could found a obvious mistake that the "EventArgs" being passed is wrong in the EventHandler. It should be "GridViewPageEventArgs" and not just "EventArgs" as shown below. VS will not alert you on this.

C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {

       }


I would recommend to use Event section of VS Properties window to create the EventHandler for you instead of coding it on your own, by double clicking on the required event, if you are not familiar with the EventArgs of a particular Event.

Hope this helps.

Thanks,
Kathir
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900