0

I am developing a web application using asp.net with c# and the above line of codes will make me to display the gridview as below. enter image description here

   DateTime startDate;
    DateTime endDate;

    if (DateTime.TryParse(txtstart.Text, out startDate) && DateTime.TryParse(txtend.Text, out endDate))
    {
        //string n1 = DropDownList2.SelectedItem.Text;

        if (DropDownList1.SelectedItem.Text == "Membership")
        {
            SqlConnection con = new
      SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select p.Name,m.FID,m.MembNo,m.MembType,m.Validity,m.Remarks,m.Organisation,m.UpdateDate from Membership_det m INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start and @End ", con);
            adapter.SelectCommand.Parameters.Add("@Start", SqlDbType.Date).Value = startDate;
            adapter.SelectCommand.Parameters.Add("@End", SqlDbType.Date).Value = endDate;
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            con.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        log.Debug("Info: Admin viewed the membership details");
    }

Now in the gridview I have UpdateDate column which is displaying the datetime because in the database I have stored it as Datetime,Now instead of Datetime I need to display only Date i.e Instead of this 11/23/2013 12:00:00 AM I need this 11/23/2013. Any suggestions are appreciated.As far as i know the data source binding can be done in aspx page but i just needed code on c#.

In aspx

  <asp:GridView ID="GridView1" runat="server" RowStyle-HorizontalAlign="Center"></asp:GridView>
1
  • You should post your asp-code too. Probably you need to add a format string in the binding of the UpdateDate column in the GridView1-control Commented Nov 26, 2013 at 10:28

4 Answers 4

1

You can use Tostring("MM/dd/yyyy") on GridView RowDataBound event by accessing the Updated Date columns of the gridview.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. You can create grid columns manually and set DataFormatString of UpdateDate column to a desired value.
  2. You can create a class for GridViews ColumnsGenerator property. And in that class when UpdateDate column is generated you can set DataFormatString.

2 Comments

I just need the code in c#,yes you are right we can do that in aspx page.
try ((BoundField)GridView1.Columns[7]).DataFormatString = "d";
0

Please convert the DateTime into the Short date format,using ToShortDateString() Method.

Better use the SQLDataReader object insted of SQLDataAdopter object;

   SqlDataReader dr=cmd.ExecuteReader();

to fill datagridview read row by row using dr.getstring('Column-No') and add it to the Datatable / Gridview Row and This is one way to do it;

Otherwise Use the below Alternative Method:


using DataAdopter itself:

  1. Take another table (DataTable) xxx
  2. From DataTable 1 (Already filled) Transfer it to Datatable2(xxx)
  3. In Between While Transfering Just change the Dateformat by DateTime.ToShortDateString() method.
  4. Finally Change DataGridview.datasource=DataTable2(xxx);

Comments

0

On RowDataBound 11/23/2013 12:00:00 AM

 void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  //change the value to `string x = dt.ToString("MM-dd-yyyy");`
     if(e.Row.RowType == DataControlRowType.DataRow)
    {

     // string time = ((Label)e.Row.FindControl("UpdateDate")).Text;
      string time =Convert.ToString(e.Row.Cells[7].Text);
      e.Row.Cells[7].Text =time.ToString("MM-dd-yyyy");`

    }
}

UPDATE

If your cell number changes according to your selected value then a better way would be format it in aspx check here and here or you can format it in your query.

1 Comment

You can see the drop down list,it has different items so the column might not be 7 every time it might change.So what might be other possibility?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.