datagridview如何显示不在数据库中的值 [英] datagridview how to show values which is not in database
本文介绍了datagridview如何显示不在数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有5列pfno,passno,relationship,status,这里要显示年龄,dob
现在我想根据dob显示状态和dob之间的人员年龄.
但是我不想在数据库中存储年龄,我想动态显示它.
假设Dob是1992年10月19日
然后在年龄下我想展示20年2个月1天我想要这样的东西
请帮助
Hi i have 5 Columns pfno, passno, relation,status,here want to display age ,dob
Now i want to display age of person between status and dob according to dob.
But i do not want to store age in database i want to show it dynamically.
suppose dob is 19/10/1992
then in age i want to display 20 years 2 mothns 1 day something like this i want
please help
推荐答案
您可以使用数据表解决此问题,
you can use data table for this issue,
public DataTable Sel()
{
DataTable dt = new DataTable();
try
{
Con_Open();
cmd = new SqlCommand("select * from Stu", con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dt.Columns.Add(new DataColumn("Reg No"));
dt.Columns.Add(new DataColumn("Name"));
dt.Columns.Add(new DataColumn("Mark1"));
dt.Columns.Add(new DataColumn("Mark2"));
dt.Columns.Add(new DataColumn("Mark3"));
while (dr.Read())
{
DataRow temp;
temp = dt.NewRow();
temp[0] = (System.Convert.IsDBNull(dr[0]) ? "" : dr[0].ToString());
temp[1] = (System.Convert.IsDBNull(dr[1]) ? "" : dr[1].ToString());
temp[2] = (System.Convert.IsDBNull(dr[2]) ? "" : dr[2].ToString());
temp[3] = (System.Convert.IsDBNull(dr[3]) ? "" : dr[3].ToString());
temp[4] = (System.Convert.IsDBNull(dr[4]) ? "" : dr[4].ToString());
dt.Rows.Add(temp);
}
cmd.Dispose();
dr.Close();
con.Close();
}
else
{
dt.Columns.Add(new DataColumn("Records"));
DataRow temp;
temp = dt.NewRow();
temp[0] = "No Records Found!!";
dt.Rows.Add(temp);
}
return dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
if (con.State == ConnectionState.Open)
{
con.Close();
}
if (!dr.IsClosed)
{
dr.Close();
}
return null;
}
}
致电:
Call:
Griview1.DataSource=Sel();
问候
sarva
regards
sarva
您可以在select语句中计算Age(在SQL或LINQ中,无论使用哪种方法,都很简单)
如果dob是DateTime:
You can calculate Age in your select statement (pretty easy in SQL or in LINQ, whichever you use)
If dob is DateTime:
SELECT pfno, passno, relation, status, DATEDIFF(year, dob, getdate()) as age, dob
如果不是日期时间,则需要对其进行转换.
If it''s not datetime, you''ll need to convert it.
在数据网格的Itemdatabound事件内部(在Gridview中,RowDataBound事件)
像DOB一样计算年龄-今天,无需存储在年龄"列中.
有关更多参考,请单击此处 [
Inside the Itemdatabound Event of the datagrid (In Gridview RowDataBound event)
calculate the age like DOB - Today and store in the age column no need.
For more reference Click Here[^]
这篇关于datagridview如何显示不在数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文