Access to dynamically bound columns

B

Bman

This seems like it should be a simple task, I have a list box loaded with
table names on a web page and a datagrid with an EditCommandColumn defined in
the aspx page. I load the selected table into the datagrid and bind the
columns at run-time and add the columns to the datagrid's column collection.
When the edit button is clicked the row is placed into edit mode with the
textboxes and everything is fine until the update button is clicked and as
soon as the post back fires, only the explicitly defined column is
accessible, datagrid.Columns.Count = 1. There are several tables in the list
so explicitly defining every column in every table in the aspx file is not an
option right now.

Any help would be appreciated,
Bill
 
B

Bman

Here is the code snippets.

Thanks,
Bman

-- aspx code--
<asp:datagrid id=dgTblMaint style="Z-INDEX: 102; LEFT: 160px; POSITION:
absolute; TOP: 120px" runat="server" Height="157px"
OnEditCommand="dgTblMaint_Edit" OnCancelCommand="dgTblMaint_Cancel"
OnUpdateCommand="dgTblMaint_Update" OnItemCommand="dgTblMaint_Command"
AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True">
<Columns>

<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" HeaderText="Edit item" ButtonType="PushButton">
</asp:EditCommandColumn>

</Columns>
</asp:datagrid>


-- aspx.cs code --
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

if(!IsPostBack)
{
// (Code to build list box)

BindTable();
}




private void BindTable()
{
if (lbxTables.SelectedIndex > -1)
{
string lsSelTblName = lbxTables.SelectedItem.Text.Trim();

if(dgTblMaint.Columns.Count <= 1)
{
ldsTid = new DataSet();
SqlDataAdapter ldaTbl = new SqlDataAdapter("SELECT * FROM " +
lsSelTblName, OpenSqlConnection(coCurConn));
ldaTbl.Fill(ldsTid, lsSelTblName);
coCurConn.Close();
ldaTbl.Dispose();

// Add blank row for insert
DataRow ldrInsertRow;
ldrInsertRow = ldsTid.Tables[lsSelTblName].NewRow();

foreach (DataColumn ldcCurCol in ldsTid.Tables[lsSelTblName].Columns)
{
ldrInsertRow[ldcCurCol.ColumnName] = DBNull.Value;
}

ldsTid.Tables[lsSelTblName].Rows.InsertAt(ldrInsertRow,
ldsTid.Tables[lsSelTblName].Rows.Count);

// Bind the columns
foreach (DataColumn ldcTblCol in ldsTid.Tables[lsSelTblName].Columns)
{
dgTblMaint = AddBoundCol(dgTblMaint, ldcTblCol);
}


dgTblMaint.DataSource = ldsTid;
dgTblMaint.DataMember = lsSelTblName;
dgTblMaint.DataBind();
}

}
}

private DataGrid AddBoundCol(DataGrid ldgCurGrid, DataColumn ldcCurCol)
{
BoundColumn lbcNewBC = new BoundColumn();
// Make first column read only
if (ldcCurCol.Ordinal == 0)
{
lbcNewBC.ReadOnly = true;
}
else
{
lbcNewBC.ReadOnly = false;
}
lbcNewBC.HeaderText = ldcCurCol.ColumnName.ToString();
lbcNewBC.DataField = ldcCurCol.ColumnName.ToString();

ldgCurGrid.Columns.Add(lbcNewBC);
return ldgCurGrid;
}


public void dgTblMaint_Update(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
object[] loaPValues;
int liIndex = 0;

// Never gets past here
if (e.Item.Cells.Count > 1)
{
foreach (TableCell loCurObject in e.Item.Cells)
{
if (loCurObject.Controls[0].GetType().Name == "TextBox")
{
loaPValues[liIndex] = loCurObject.ToString();
liIndex += 1;
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top