• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ guy doing aspx c# page and need help

HumblePie

Lifer
okay I have a SQL database at a remote IP. I connect, grab a dataset out of it, take my gridview and set the datasource to the dataset and bind.

Then I can sort by turning on the sort attribute and the onsorting event handler.

All peachy so far. Now, I'm trying to add a up and down arrow icon to the header cells of each column in the gridview and no dice.

Why? Despite my gridview displaying a full table with columns and rows, when I look at the column count of my gridview I get ZERO. My dataset shows 9 columns, my gridview displays 7 of those, since 2 columns contain binary image data which I'm still working on displaying, but the actual count it shows is zero. So, when I try to get the onrowcreate event, to find my column headers when I do a sort so I can attach an image url to them, I can't.

Since this is a DB that can constantly change, I can't just set my gridview control with a predefined set of unbound columns. Not going to work. Any clues why gridview is showing zero columns? It does do my row count for data only rows just fine though.
 
Some of the code

<asp:GridView ID="gvResults" runat="server" AllowSorting="true" OnRowCreated="onRowCreated" onSorting="onSort"></asp:GridView>




private DataSet GetData()
{
SqlConnection cDbConn = null;
SqlDataAdapter cDA = null;
DataSet cSet = null;
string strCmd;

try
{
cDbConn = new SqlConnection("server=blah IP address;"
+ "database=Blah database;"
+ "User Id=blah ID;"
+ "Pwd=blah Password;");
cDbConn.Open();
}
catch (SqlException excSql)
{
Label2.Visible = true;
Label2.Text = "<strong>ERROR!</strong>. "
+ "Unable to connect to SQL server: "
+ excSql.Message;
return null;
}

Label1.Text = "Connected to SQL Server";

try
{
strCmd = (string)HttpContext.Current.Session["SqlCommand"];
cDA = new SqlDataAdapter(strCmd, cDbConn);
if (strCmd.Contains("BETWEEN") == false)
{
SqlParameter param = cDA.SelectCommand.Parameters.AddWithValue("@param",
(string)HttpContext.Current.Session["SqlParam"]);
}
else
{
SqlParameter param = cDA.SelectCommand.Parameters.AddWithValue("@param",
(DateTime)HttpContext.Current.Session["SqlParam"]);
SqlParameter param2 = cDA.SelectCommand.Parameters.AddWithValue("@param2",
(DateTime)HttpContext.Current.Session["SqlParam2"]);
}

cSet = new DataSet();
cDA.Fill(cSet);
}
catch (SqlException excSql)
{
Label2.Visible = true;
Label2.Text = "<strong>ERROR!</strong>. "
+ "Unable to retrieve data from SQL Server: "
+ excSql.Message;
cDbConn.Close();
return null;
}

cDbConn.Close();
Label2.Text = "";
Label2.Visible = false;

gvResults.DataSource = cSet;
gvResults.DataBind();

Label1.Text = gvResults.Columns.Count.ToString() + " " + cSet.Tables[0].Columns.Count.ToString() + " " + gvResults.Rows[0].Cells[0].Text + " " + cSet.Tables[0].Rows.Count.ToString();

return cSet;
}



Basically right at this point, I'm using my Labels on my page to display some debugging info for me as I code. the gridview right after binding to the dataset or anything else will show zero columns despite the actual grid on the page having all the columns. Any clue how to solve this?
 
Doesn't work, unfortunately. As per the MSDN reference, automatically generated columnns in a datagridview are not added to the columns collection.


If you want to access them programatically, you'll have to forgo automatic addition and add them manually.

Adding a simple bound text column isn't too hard. Create a new BoundField, set the Datafield to the relevant data column name, and set the header text, so forth, then add it to the columns collection.



Of course, figuring out that you need to do that takes a damned hour on google. And figure out why on earth they don't add automatically generated columns to the columns collection requires the oracle at Delphi.
 
Originally posted by: PhatoseAlpha
Doesn't work, unfortunately. As per the MSDN reference, automatically generated columnns in a datagridview are not added to the columns collection.


If you want to access them programatically, you'll have to forgo automatic addition and add them manually.

Adding a simple bound text column isn't too hard. Create a new BoundField, set the Datafield to the relevant data column name, and set the header text, so forth, then add it to the columns collection.



Of course, figuring out that you need to do that takes a damned hour on google. And figure out why on earth they don't add automatically generated columns to the columns collection requires the oracle at Delphi.


Hell, it took longer than an hour, since I spent ALL DAMN DAY looking around on google and MSDN trying to figure out why no columns were in the collection. Coming as a c++ app guy to doing this is a pain in the rear. Took me nearly a day to figure out there is no such thing as persistent data unless you cache something, or put it into a session or viewstate. Hrmm, since auto generate doesn't add the columns to the collection, going to have to rethink.
 
Back
Top