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

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
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.
 

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
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?
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
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.
 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
Where exactly are you trying to get the count? In Page_Load?

Have you tried:
int count = myGridView.Rows[0].Cells.Count;
 

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
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.