Click here to Skip to main content
15,911,891 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi there
I want to generate reports of student attendance according to date that user selects.suppose user selects date 1/1/2012 to 10/1/2012 then 1 datagridview should be get generated dynamically. Plz help me.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-12 3:01am    
What have you tried so far? What's the problem?
--SA

Filter the data as part of your SELECT statement:
SQL
SELECT * FROM myTable WHERE dateColumn BETWEEN '2011-1-1' AND '2011-1-10'

Then use that as part of your DataAdapter, and use the Fill method to prepare a DataSet or DataTable.
You can then use that as the DataSource property of your DataGridView, and it will sort it all out for you.
 
Share this answer
 
Comments
SantoshRohinSantosh 27-Jan-12 3:48am    
Will you please elaborate.
OriginalGriff 27-Jan-12 3:50am    
Which bit?
SantoshRohinSantosh 27-Jan-12 3:56am    
You just tell me GRIFF how i'l give title to the datagridview?
OriginalGriff 27-Jan-12 4:04am    
If you fill a DataGridView from a DataAdapter, then whatever column titles you return from your database will be set as the Table Column titles:
SELECT Id, username AS [User Name] From MyTable
Will give you two columns, with the titles "Id" and "User Name"
Assume that your results from database is put on list, and each row of results is put on Student class.
C#
IList<student> listStudent</student>


Assume that Student class is below:
C#
public class Student {
  public string StudentID {set; get;}
  public string StudentName {set; get;}
}


In aspx file, put tag gridview, see the below for example:
ASP
<asp:gridview runat="server" id="gvStudent" autogeneratecolumns="False" xmlns:asp="#unknown">
  AllowPaging="True" AllowSorting="False" EmptyDataText="No Data Found"
  HorizontalAlign="Center" Width="100%" DataKeyNames="StudentID " PageSize="20">

  <emptydatarowstyle horizontalalign="Center" verticalalign="Middle" width="100%" />
  <columns>
    <asp:templatefield headertext="No.">
      <headerstyle width="5%"></headerstyle>
      <itemtemplate>
        <asp:label id="Label2" runat="server" name="No" text="<%# (gvStudent.PageIndex * gvStudent.PageSize) + gvStudent.Rows.Count + 1 %>">
        </asp:label>
      </itemtemplate>
      <itemstyle horizontalalign="Center" />
    </asp:templatefield>
    <asp:boundfield headertext="Student ID" datafield="StudentID" />
    <asp:boundfield headertext="Student Name" datafield="StudentName" />
  </columns>
</asp:gridview>


And in code behind, see the below for the example:
C#
private void BindGridView()
{
  //Retrieve student from database, and put on list
  IList<student> listStudent = ......

  //Bind Result to GridView
  this.gvStudent.DataSource = listStudent;
  this.gvStudent.DataBind();
}
</student>


And call method BindGrindView on button click

Or you can see from other tutorial about gridview in codeproject http://www.codeproject.com/Articles/14249/How-to-populate-DataGridView-GridView-with-SQL-sta[^]
 
Share this answer
 
Comments
SantoshRohinSantosh 27-Jan-12 4:04am    
I am building windows application sir.
IkhwanKrisnadi 27-Jan-12 4:15am    
oooo i see, I havent ever use GridView on windows application, but I think it is same ...may you could see about GridView in Windows Application on http://www.dotnetcurry.com/ShowArticle.aspx?ID=132

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