如何使用数据网格视图在文本框中显示日期数据 [英] how to display date data in text box using data grid view

查看:81
本文介绍了如何使用数据网格视图在文本框中显示日期数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表格,其中有1个标签开始日期以及相应的文本框.我通过ADD按钮(使用插入查询)将值插入了数据库.我采取了datagrid视图显示数据.现在的问题是我想要当我选择一行datagridview时,它应该在相应的文本框中显示数据.我尝试了以下代码,但无法在日期字段中获得所需的结果

i have created a form in which there is 1 label starting date along with respective textbox.i have inserted the value in the database through ADD button (using insert query).I have taken datagrid view to display the data .The problem is now i want that when i select one row of datagridview then it should display data in respective textbox. i have tried following code but not able to get desired result for date field

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
          SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Initial Catalog=PSM;Integrated Security=True");
          con.Open();

          SqlCommand com = new SqlCommand("select * from Publicity_Threshold  where Threshold_id=@Threshold_id=''" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() + "''", con);
          SqlDataReader dr = com.ExecuteReader();
          if (dr.HasRows)
          {
              dr.Read();
              txtThresholdID.Text = dr[0].ToString();
              cboThreholdname.Text = dr[1].ToString();
              cbomethodid.Text = dr[2].ToString();
              cbomediaused.Text = dr[3].ToString();
             startingdate.Value = dr[4].ToString();                    }
          dr.Close();
          con.Close();
      }


错误只在这
startingdate.Value = dr [4] .ToString();
您可以纠正我吗


error is in this only
startingdate.Value = dr[4].ToString();
can u rectify me

推荐答案

如果startingdate是datetimepicker,请尝试以下操作:

if startingdate is a datetimepicker try this:

startindate.Value = dr[4];




如果您的控件是DateTimePicker,则使用此语法

Hi,

If your control is DateTimePicker then use this syntax

startingdate.Value = Convert.ToDateTime(dr[4].ToString());



试试这个代码

问候
AR



Try this code

Regards
AR


尝试使用此

try with this

startingdate.Text = dr[4].ToString();


这篇关于如何使用数据网格视图在文本框中显示日期数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆