locked
GridView populating dates from Calendar control RRS feed

  • Question

  • User-400063098 posted

    i have created a form where user select date from date picker, now i want to generate a sheet in grid view of selected month and year from datepicker, i want to populate my gridview with selected month and year. All work i done in asp.net with master pages. actually i am making manhour calculator,user have to fill that sheet and submit it to supervisor. please help me. :( :(

    Wednesday, January 31, 2018 9:11 AM

All replies

  • User283571144 posted

    Hi sehru,

    According to your description, I couldn't understand your requirement clearly.

    What do you mean sheet in gridview?

    Do you mean you want to search the data from database according to the date and fill the data into gridview?

    I suggest you could post the codes you have used now and the details description of your requirement.

    Best Regards,

    Brando

    Thursday, February 1, 2018 6:40 AM
  • User-400063098 posted

    //int month = Convert.ToInt32(expo_date.Text);
    //int year = DateTime.Now.Year;
    int days = GetDaysInMonth(Convert.ToInt32(expo_date.Text), Convert.ToInt32(DateTime.Now.Year));
    //int days = System.DateTime.DaysInMonth(year, month);
    int year = DateTime.Now.Year;
    int month = DateTime.Now.Month;
    //int days = DateTime.DaysInMonth(year, month);
    DataTable dt = new DataTable();
    DataRow dr = null;

    dt.Columns.Add(new DataColumn("strDate", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));

    for (int i = 1; i <= days; i++)
    {

    string date = i.ToString() + "-" + month.ToString() + "-" + year.ToString();

    dr = dt.NewRow();
    dr["strDate"] = date;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;

    dt.Rows.Add(dr);
    }
    ViewState["CurrentTable"] = dt;

    gvdetail.DataSource = dt;
    gvdetail.DataBind();

    }

    this is my code and want to bind selected month and year with gridview.

    i just want to bind user input (month and year) with gridview.

    Friday, February 2, 2018 6:04 AM
  • User283571144 posted

    Hi sehru,

    According to your description, I guess you want to add the date as new row into gridview.

    If this is your requirement, I suggest you could add runat server in the datepicker, then you could get the datepicker's date in the code-behind.

    At last you could add the new row according to the datepicker's date.

    More details, you could refer to blow codes:

    ASPX:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
      <link rel="stylesheet" href="/resources/demos/style.css"/>
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script>
      $( function() {
        $( "#datepicker" ).datepicker();
      } );
      </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <p>Date: <input type="text" id="datepicker" runat="server"/></p>
    
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
            </div>
        </form>
    </body>
    </html>

    Code-behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    SetInitialRow();
                }
    
             
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    DataRow drCurrentRow = null;
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow[0] = datepicker.Value;
                    drCurrentRow[1] = datepicker.Value;
                    drCurrentRow[2] = datepicker.Value;
    
                    dtCurrentTable.Rows.Add(drCurrentRow);
    
                    ViewState["CurrentTable"] = dtCurrentTable;
                    GridView1.DataSource = dtCurrentTable;
                    GridView1.DataBind();
    
                }
                else
                {
                    Response.Write("ViewState is null");
                }
    
    
            }
    
    
            private void SetInitialRow()
            {
                DataTable dt = new DataTable();
                DataRow dr = null;
                dt.Columns.Add(new DataColumn("Date", typeof(string)));
                dt.Columns.Add(new DataColumn("Column1", typeof(string)));
                dt.Columns.Add(new DataColumn("Column2", typeof(string)));
                 dr = dt.NewRow();
                dr["Date"] = DateTime.Now.ToString();
                dr["Column1"] = string.Empty;
                dr["Column2"] = string.Empty;
                dt.Rows.Add(dr);
                ViewState["CurrentTable"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }

    Result:

    Best Regards,

    Brando

    Wednesday, February 7, 2018 2:26 AM