Tuesday, June 30, 2009

C#: Using comboBoxes in dataset/datatable bound dataGridViews

//Build fkey combobox 
var fKeyCombo = new DataGridViewComboBoxColumn
// most of these are frequently string values or constants 
HeaderText = "States", // The column header text 
DisplayMember = "StateAbbreviation",
// what field from the fkey table to display (list of names) 
Name = "state", // will be the name you use when you reference the column in the dataGridView 
ValueMember = "Id", // fkey link column typically the column in the foreign key table you join the main table on 
DataPropertyName = "stateId" // main table field you are replacing with a comboBox 
BindingSource fkey1Binding = new BindingSource();
var Fkey1Table = db.getDs().TechObjTypes;
fkey1Binding.DataSource = Fkey1Table; fKeyCombo.DataSource = fkey1Binding;
//Add other fkey columns here or just delete below, and use the snippet over again  
//Sets up the main Table for dataGridView binding comment out or delete if using multiple fkey snippets //best declared with a larger scope than this function so it can later be submitted to the tableAdapter for updates
var mainTable = DataSet1.mainTable();
var MainBinding = new BindingSource();
MainBinding.DataSource = mainTable;
dataGridView1.DataSource = MainBinding;
//Must call MainBinding.EndEdit(); after updates, so it will update the MainTable 
// then you can pass the mainTable to a dataAdapter to do all the updates. 

Sunday, June 28, 2009

Regular Expressions :Regex.Replace()

I now have a coding blog, yay!

Today I learned how to match and replace at the same time in a Regex:

'remove leading = if it exists
lines(index) = Regex.Replace(lines(index), "(?\s*)=(?.+)", "${space}${therest}")
This makes use of named captures to remove a leading =, preserving opening whitespace.