开发者

Can I use a DataGridView in a Windows Service?

I created a Windows Forms app that generates reports and it works great. It binds a bunch of DataTables to a bunch of DataGridViews, exports the resulting display to a bitmap, and everyone is happy. I would like to create a service that generates these reports automagically instead of having to run each report by hand in my Forms based app. I am running into a problem where I have a DataTable with rows in it, but when I assign it to be the datasource for one of my DataGridViews, the number of rows in the DataGridView remains at zero.

Here is a snippet of relevant code where I am binding this data (yet it is not updating):

DataGridView testGrid = new DataGridView();
testGrid.BackgroundColor = Color.Yellow;

for (int i = ServerTableDay.Count - 1; i >= 0; i--)
{
testGrid.DataSource = ServerTableDay[i];
}

The test grid always has zero rows no matter which table I try to bind by using the .DataSource =

What am I missing, or is this even possible in a Wi开发者_C百科ndows Service?


Although not ideal, you can use a DataGridView inside a Windows Service (just reference System.Windows.Forms). I just tested in Service app, and it works fine. I say "not ideal" because there's a lot of overhead that comes with a DataGridView, most of it being that the control is really a visual control, which isn't needed in a Windows Service application.

The real question is: What type of object is ServerTableDay? It's not a System.Data.DataTable (like you allude to), because you can't access the index (e.g. ServerTableDay[i]) of a DataTable like that. Also, verify in debugging that there is data in your ServerTableDay object.


Based on your heading - No you will not be able to use a DataGridView with a Windows Service because this is a User Interface control and the Windows Service would have no UI control. That being said you might look into writing your output to a logfile if you want to keep track of it.

As for your current winforms setup issue try -

if (ServerTableDay.Count > 0) {
     testGrid.DataSource = ServerTableDay;
}


Just try

testGrid.DataSource = ServerTableDay;

I don't think you need to do a loop. Actually you musn't do a loop. Just tell the datagridview that its datasource is your datatable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜