开发者

C# problem with filling datagrid with multiple datasource

HEy Friends I have datagridview in my program. I need to fill datagrid from database but One column i.e, named "TIME" i have to fill it from the datetime picker present in same form. There in LOAD button, when i click it, datagridview should be populated 开发者_开发百科with records from database along with the value of datatime picker in the Column named TIME. so i will be very thankful if anyone send the solution.


you can set datetimepicker value to sqlcommand as below:

select field1, field2, ... , datetimepicker.value
from table_name
where your_conditions

test it please. And i think it will faster than adding new column to datatable and set its value to datetimepicker.value after datatable filled


You can do this as follows if you are using DataSet/DataAdapter to bind the Grid:

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter();
    da.Fill(ds);
    DataTable dt = ds.Tables[0];

    DataColumn dc = new DataColumn("TIME", typeof(System.DateTime));
    dc.DefaultValue = dataTimePicker.SelectedDate;

    dt.Columns.Add(dc);
    grdView.DataSource = dt;
    grdView.DataBind();


I think you could use the data binding events to add your extra column. Depending on your framework version and type of grid (Datagrid or Gridview) the events differ slightly.

The basic idea is the event is called each time a row is added to the grid and you dynamically add your own value into another column you specify. Sorry I can't offer more than this but it's all I can based on what you've presented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜