Scott Kellish

I have a dataset with fields (duedate, title, author, isbn) tied to an ownerdraw listbox so that I can format all of the fields in a single row as in:

DataSet ds = lda.GetItems(1833);
listBox1.DataSource =
new BindingSource(ds, "Items");

----------------------------------------------------
Due: 1/1/2007

History of the Decline and Fall of
the Roman Empire
, Edward Gibbon
ISBN: 12-23456-89
-----------------------------------------------------

What is the syntax (C#) for accessing the individual row cells in the bindingsource from the drawitem event



Re: Windows Forms Data Controls and Databinding How to access currencymanager from listbox DrawItem event

Ken Tucker

The listbox item will contain a datarowview with the information. Here is a simple example that just draws the Product Name



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ODList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SqlCeConnection conn = new SqlCeConnection("data source='c:\\northwind.sdf'; mode=Exclusive;");
SqlCeDataAdapter da = new SqlCeDataAdapter("Select * from Products", conn);
DataTable dt = new DataTable();
da.Fill(dt);

listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DataSource = dt;
listBox1.DisplayMember = "Product Name";
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
string s;
DataRowView drv = (DataRowView)listBox1.Items[e.Index];
s = drv["Product Name"].ToString();
g.FillRectangle(Brushes.White, e.Bounds);

if((e.State & DrawItemState.Selected) == DrawItemState.Selected )
{
g.FillRectangle(Brushes.LightBlue, e.Bounds);
}

g.DrawString(s, listBox1.Font, Brushes.Black, e.Bounds);

}
}
}