开发者

C# how to display datetimepicker control for all rows of gridview

In my application I am creating rows and columns dynamically. I created a column of type System.DateTime. After this i want to display datetimepicker control for all rows in that column.

I created a column using

dataTable.Columns.Add("CreatedOn", Typ开发者_JS百科e.GetType("System.DateTime"));

and i am adding rows as

foreach(String filename ......)
 dataTable_FileProperty.Rows.Add(filename,//here i want to add dateTimePicker

So, what is a solution for this.

EDIT: Please provide some code snippet. I am new to C#.net.

Thanks.


By default you have just these columns available for you:

DataGridViewTextBoxColumn, DataGridViewCheckBoxColumn, DataGridViewImageColumn, DataGridViewButtonColumn, DataGridViewComboBoxColumn, DataGridViewLinkColumn

If you want to show a datetimepicker control then you have to implement a custom column.

Check this out: http://msdn.microsoft.com/en-us/library/7fb61s43.aspx

Hope this helps.


use the item template of the gridview and place a datetimepicker there. A good example is here

For implementing it you have to implement the ITemplate interface.

Another example is this

An easy implementation of it is given in this msdn article. But the code is in VB.net.


DataTable.Rows contains data, i.e file name, date, some strings.

GridView.Columns contains controls to display data.

So if you're using DataRowCollection.Add(Object[]) :

DataTable DataTable1 = new DataTable();
DataTable1.Columns.AddRange(
     new DataColumn[] {
          new DataColumn("file", typeof(string)),
          new DataColumn("date", typeof(DateTime)) });

foreach (string f in System.IO.Directory.GetFiles(@"c:\windows"))
    DataTable1.Rows.Add(f, System.IO.File.GetCreationTime(f));

GridView1.DataSource = DataTable1;
GridView1.DataBind();

And the markup of GridView:

<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="File" DataField="file" />
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Calendar runat="server" ID="Calendar1" SelectedDate='<%# Bind("date") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Also you need to read more about Calendar.SelectedDate and Calendar.VisibleDate

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜