Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Stupid DataGrid Question
 

Stupid DataGrid Question

This is my first attempt at using a datagrid and I'm having MAJOR problems.  I can't get the grid to show up on my webpage! 
Everything seems to be working fine but NO GRID!  I've followed all the code examples I could find but to no avail! Everything is fine.  The connection opens, the data is returned but the no grid where it should be on the page its just blank!

PLEASE can someone point out what I'm doing wrong!!!!

See code below

I have the following code in my .apsx file

asp:DataGrid id="dgridCustomers" style="Z-INDEX: 106; LEFT: 72px; POSITION: absolute; TOP: 240px" runat="server"


and the following function is called from an event on the server triggered by a button

private void UseDataAdapter()
{
SqlConnection conn =
null;
bool isOpen = false;
try
{
conn =
new SqlConnection(this.Session["SQL_CONNECTION"].ToString());
conn.Open();
isOpen =
true;
DataTable tbl =
new DataTable("Customers");
SqlDataAdapter da =
new SqlDataAdapter("select * from Customers",conn);
da.Fill(tbl);

// do this just to check if we got good data back!
for
( int i = 0; i < tbl.Rows.Count; i++)
{
Response.Write(tbl.RowsIdea[0] + "<br />");
}

dgridCustomers.DataSource = tbl.DefaultView;
dgridCustomers.DataMember = "Customers";
dgridCustomers.Visible =
true;

goto RETURN;

}

catch( SqlException sEx )
{
lblMessage.Text = sEx.Message;
goto RETURN;
}

RETURN:
if( isOpen )
conn.Close();

}

Mark Toth  Tuesday, July 05, 2005 2:54 AM

This is a Windows Forms forum and this is an ASP.NET question - a much better place for this type of question is here: http://forums.asp.net/

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe Stegman  Wednesday, July 06, 2005 8:56 PM
Ok I got this to work but now cant get the edit column events to fire No events fire for the datagrid at all not a sort event I tried to all not any edit button events at all.  I just forgot to call DataBind() function after editing the data source.  OK now it works great and I moved on the attempting to add sorting and editing to the grid and I can't get the events to fire off at all. Nothing will fire not a OnSortCommand event or and OnEditCommand Event will fire nothing!  I'm using Visual Studio.Net 2003 and I noticed when I added the event through the property browser that it didn't add it to the control on the aspx page so I added it manually in the HTML code here's the code:

asp:datagrid id="dgridCustomers" style="Z-INDEX: 106; LEFT: 72px; POSITION: absolute; TOP: 240px" runat="server" Width="85%" Font-Names="Arial" Font-Size="X-Small" BackColor="AliceBlue" BorderColor="Gray" HorizontalAlign="Center" AutoGenerateColumns="False" OnEditCommand="EditRow"

It did how ever add to to the InitializeComponent fucntion in the .cs so I figuered I was good to go but the event will not fire off!  nothing happens.  I placed a break point in the event and it never gets reached.  Here is the event code:

public void EditRow(object sender, DataGridCommandEventArgs e)

{

this.dgridCustomers.EditItemIndex = e.Item.ItemIndex;

int readType = Convert.ToInt16(rdoReadTypes.SelectedValue.ToString());

switch( readType )

{

case 1:

UseDataReader();

break;

case 2:

UseDataAdapter();

break;

case 3:

UseDataBinding();

break;

}

}

and just for those who are curious here in my On_Load event

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

menu = LoadControl("mainmenu.ascx");

// run through the controls on the page and place this at the top of the form

Page.FindControl("Form1").Controls.AddAt(0, menu );

((mainmenu)menu).DataGridButton.BackColor = System.Drawing.Color.White;

if( !this.IsPostBack )

UseDataReader();

}

Does anyone have any ideas what's going astray here?????? Again for the second time I'm stumped Arrrrrrg!

Mark Toth  Tuesday, July 05, 2005 6:40 AM
Can you post the event handler addition code that is present in the Initialize Component Section.

Regards,
Kunal
Kunal Yadav  Tuesday, July 05, 2005 7:23 AM

here is the initialize component code
hope this helps!

private
void InitializeComponent()

{

this.btnGetData.Click += new System.EventHandler(this.GetData_Click);

this.dgridCustomers.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.EditRow);

this.Load += new System.EventHandler(this.Page_Load);

}

Mark Toth  Tuesday, July 05, 2005 7:27 AM

Try it with this method :

The datagrid tag in the aspx page should be having "OnEditCommand="dgridCustomers_Edit"

Register the event as
dgridCustomers.EditCommand +=
             new DataGridCommandEventHandler(this.dgridCustomers_Edit);

If this is what you have done then it should work and maybe you can try it on a different page if the current page is not working. Create a new page, add a datagrid register the event and see what happens.

Regards,
Kunal

Kunal Yadav  Tuesday, July 05, 2005 7:44 AM
Ok I tried what you suggested and the same thing happened.  No events fired off so just for laughs I didn't add my user control to the form on the new page
and guess what.  Now the events work!  Strange happenings!

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

// Put user code to initialize the page here

menu = LoadControl("mainmenu.ascx");

// run through the controls on the page and place this at the top of the form

//Page.FindControl("Form1").Controls.AddAt(0, menu );

((mainmenu)menu).DataGridButton.BackColor = System.Drawing.Color.White;

if( !this.IsPostBack )

UseDataReader();

}

So I'm gonna do a bit of experimenting with the user control since its the menu for the pages so I need it!  but its still very wierd that the user control would interfere with the events for the datagrid.  Nothing in the two are named the same which leades me to believe its where I'm adding the control in the form that is offensive since technically the control is loaded and I'm setting a property of it and not causing sometype of error! Anyway, if you have any suggestions I appreciate it and thanks for the help.  Another Microsoft quirk I guess!

;)

Mark Toth  Tuesday, July 05, 2005 8:51 AM

This is a Windows Forms forum and this is an ASP.NET question - a much better place for this type of question is here: http://forums.asp.net/

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe Stegman  Wednesday, July 06, 2005 8:56 PM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView setup problem
• Show text in text box based on selection from combo box
• DataGridView problem
• Rowheader and Row Number
• SelectedValue DGVComboBoxCell??
• DataGridView button click---Entered value disappears
• TableAdapter.Fill Problem - No mapping from DBType to SqlDBType
• How to display memo field in datagridview?
• DataGridView ErrorIcon for Constraint errors
• [DataGridView] Jumping to a row on key press