I am creating a custom control which takes in a filepath (string) to an xml file and a dictionary of column headers, column fields and creates a gridview dynamically with CRUD operations. What I want to know is how to replicate/achieve Text='<% #Bind("field") %>' by only using code behind?
I was thinking of trying:
Dictionary<string, string> columns = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> column in columns)
{
BoundField bField = new BoundField();
bField.DataField = column.Value;
bField.HeaderText = column.Key;
GridView1.Columns.Add(bField);
}
I'm open to suggestions as to either using .DataField or Text='<% #Bind("field") %>' or any way I haven't thought of if it achieves the end goal. As with the CRUD, can anyone recommend a good way to do this? Maybe dynamically inserting textbox and label controls into the gridview? I am using Visual Studio Express 2013 for Web.
You override the ItemDataBound event. If you're thinking about dynamically adding controls to each row, you're better off using a different kind of templated control such as a Repeater.
UPDATE - an example
First make sure you handle the RowDataBound event (not ItemDataBound my bad, that's for the DataGrid): -
Set up your columns from your dictionary of column names (I've used a string array, but you get the idea): -
Then in the RowDataBound event: -