473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Datagrid fails to show data!

Hi,
I have a datagrid that is ment to insert data. But when I run the form
only the header appears. I would like some advise from you all and
solve this problem I'm using visual studio 2003. My code looks like
this:

private void Fill()
{
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * from
dbo.DashBoard", con);
adapter.Fill(table);
}
private void Bind()
{
DataTable table = new DataTable();
dgis.DataSource = table;
dgis.DataBind();
}
private void InsertEmpty()
{
DataTable table = new DataTable();
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[5];
ecc.UpdateText = "Insert";
EditCommandColumn ecc1 =(EditCommandColumn) dgis.Columns[4];
ecc.UpdateText = "Insert";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
EditCommandColumn ecc1 =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
break;

case "Edit":
// begin editing
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
Bind();
}

Feb 27 '07 #1
6 3690
Hi there,

Rocco, have a look at your code again :) Your binding to empty table:
private void Bind()
{
DataTable table = new DataTable();
dgis.DataSource = table;
dgis.DataBind();
}

i know it may be something new for you but if you declare a variable in a
method, variable is seen only within this function regardless if the same
name is used elswhere.

it might be done like this:

private DataTable GetData()
{
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * from dbo.DashBoard", con);

try
{
con.Open();
adapter.Fill(table);

}
catch
{
throw;
}
finally
{
con.close();
}

return table;
}

private DataTable GetDataWithEmptyRow()
{
DataTable table = GetData();

table.Rows.InsertAt(0, table.NewRow());

return table;
}
Now you may use this functions in your code.
--
Milosz
"rcoco" wrote:
Hi,
I have a datagrid that is ment to insert data. But when I run the form
only the header appears. I would like some advise from you all and
solve this problem I'm using visual studio 2003. My code looks like
this:

private void Fill()
{
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * from
dbo.DashBoard", con);
adapter.Fill(table);
}
private void Bind()
{
DataTable table = new DataTable();
dgis.DataSource = table;
dgis.DataBind();
}
private void InsertEmpty()
{
DataTable table = new DataTable();
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc = (EditCommandColumn) dgis.Columns[5];
ecc.UpdateText = "Insert";
EditCommandColumn ecc1 =(EditCommandColumn) dgis.Columns[4];
ecc.UpdateText = "Insert";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}
private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
EditCommandColumn ecc1 =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Update";
break;

case "Edit":
// begin editing
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
Bind();
}

Feb 27 '07 #2
Thanks Milosz,
I've managed to work it out. But I got another problem i cannot create
a new row to insert new data. I actually created a button: bttnew, And
added the Update, Edit and cancel filled. But at the press of button
nothing changes. this is the code I'm actually using now.

private void InsertEmpty()
{
DataTable table = new DataTable();
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Insert";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}

private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
dgis.EditItemIndex = -1;
break;

case "Edit":
// begin editing
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
Bind();
}

Thanks very much.

Feb 27 '07 #3
You're are still operating on local variables in each function so you reasign
data source in Fill() method.
--
Milosz
"rcoco" wrote:
Thanks Milosz,
I've managed to work it out. But I got another problem i cannot create
a new row to insert new data. I actually created a button: bttnew, And
added the Update, Edit and cancel filled. But at the press of button
nothing changes. this is the code I'm actually using now.

private void InsertEmpty()
{
DataTable table = new DataTable();
table.Rows.InsertAt(table.NewRow(), 0);
}

private void bttnew_Click(object sender, System.EventArgs e)
{
// inserting
dgis.EditItemIndex = 0;

// modify text
EditCommandColumn ecc =(EditCommandColumn) dgis.Columns[0];
ecc.UpdateText = "Insert";

// fill table, insert empty row, bind to datagrid
Fill();
InsertEmpty();
Bind();
}

private void dgis_ItemCommand(object
source,System.Web.UI.WebControls.DataGridCommandEv entArgs e)
{
// stop editing
dgis.EditItemIndex = -1;

switch (e.CommandName)
{
case "Insert":
break;

case "Update":
break;

case "Cancel":
dgis.EditItemIndex = -1;
break;

case "Edit":
// begin editing
dgis.EditItemIndex = e.Item.ItemIndex;
break;
}

// fill and bind
Fill();
Bind();
}

Thanks very much.

Feb 27 '07 #4
Thanks Milosz,

It still did not work actually when I run the form one row from
database becomes too rows in the datagrid. Still the add button does
not function. When I press it no row is created. What could be the
problem?

Thanks

Feb 28 '07 #5
Hi there,

Handle RowEidting event and set grids view EditIndex property not
e.NewEditIndex. I prepared fully working example:

<asp:GridView runat="server" ID="dg" AutoGenerateColumns="false"
OnRowEditing="dg_RowEditing"
ShowFooter="true" OnRowUpdating="dg_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtName" Text='<%# Bind("Name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:Button runat="server" CommandName="Edit" ID="btnAddNew" Text="New" />
<asp:Button runat="server" CommandName="Update" ID="btnUpdate"
Text="Update" Visible="false" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dg.DataSource = GetData();
dg.DataBind();
}
}

private System.Data.DataTable GetData()
{
System.Data.DataTable dataTable =
new System.Data.DataTable();

dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));

for (int i = 0; i < this.RowCount; i++)
{
System.Data.DataRow r = dataTable.NewRow();
r[0] = i;
r[1] = "name" + i.ToString();
dataTable.Rows.Add(r);
}

return dataTable;
}

protected void dg_RowEditing(object sender, GridViewEditEventArgs e)
{
System.Data.DataTable dataTable = GetData();
dataTable.Rows.InsertAt(dataTable.NewRow(), 0);

GridView gridView = (GridView)sender;
gridView.EditIndex = 0;

dg.DataSource = dataTable;
dg.DataBind();

Button button;
button = (Button)gridView.FooterRow.FindControl("btnUpdate" );
button.Visible = true;
button = (Button)gridView.FooterRow.FindControl("btnAddNew" );
button.Visible = false;
}
protected void dg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gridView = (GridView)sender;
TextBox textBox = (TextBox)gridView.Rows[0].FindControl("txtName");

string newName = textBox.Text;

// do something with name and insert to database

// simulate a new row has been added
RowCount++;

// go back to readonlt mode
gridView.EditIndex = -1;
gridView.DataSource = GetData();
gridView.DataBind();
}

/// <summary>
/// I only use it to simulate new record was added
/// </summary>
private int RowCount
{
get
{
object value = ViewState["RowCount"];
return value == null ? 5 : (int)value;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
ViewState["RowCount"] = value;
}
}
</script>
Hope it helps
--
Milosz
"rcoco" wrote:
Thanks Milosz,

It still did not work actually when I run the form one row from
database becomes too rows in the datagrid. Still the add button does
not function. When I press it no row is created. What could be the
problem?

Thanks

Mar 1 '07 #6
Hi there,

Handle RowEditing event and set grids view EditIndex property not
e.NewEditIndex. I prepared fully working example:

<asp:GridView runat="server" ID="dg" AutoGenerateColumns="false"
OnRowEditing="dg_RowEditing"
ShowFooter="true" OnRowUpdating="dg_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtName" Text='<%# Bind("Name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:Button runat="server" CommandName="Edit" ID="btnAddNew" Text="New" />
<asp:Button runat="server" CommandName="Update" ID="btnUpdate"
Text="Update" Visible="false" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dg.DataSource = GetData();
dg.DataBind();
}
}

private System.Data.DataTable GetData()
{
System.Data.DataTable dataTable =
new System.Data.DataTable();

dataTable.Columns.Add("Id", typeof(int));
dataTable.Columns.Add("Name", typeof(string));

for (int i = 0; i < this.RowCount; i++)
{
System.Data.DataRow r = dataTable.NewRow();
r[0] = i;
r[1] = "name" + i.ToString();
dataTable.Rows.Add(r);
}

return dataTable;
}

protected void dg_RowEditing(object sender, GridViewEditEventArgs e)
{
System.Data.DataTable dataTable = GetData();
dataTable.Rows.InsertAt(dataTable.NewRow(), 0);

GridView gridView = (GridView)sender;
gridView.EditIndex = 0;

dg.DataSource = dataTable;
dg.DataBind();

Button button;
button = (Button)gridView.FooterRow.FindControl("btnUpdate" );
button.Visible = true;
button = (Button)gridView.FooterRow.FindControl("btnAddNew" );
button.Visible = false;
}
protected void dg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gridView = (GridView)sender;
TextBox textBox = (TextBox)gridView.Rows[0].FindControl("txtName");

string newName = textBox.Text;

// do something with name and insert to database

// simulate a new row has been added
RowCount++;

// go back to readonlt mode
gridView.EditIndex = -1;
gridView.DataSource = GetData();
gridView.DataBind();
}

/// <summary>
/// I only use it to simulate new record was added
/// </summary>
private int RowCount
{
get
{
object value = ViewState["RowCount"];
return value == null ? 5 : (int)value;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
ViewState["RowCount"] = value;
}
}
</script>
Hope it helps
--
Milosz
"rcoco" wrote:
Thanks Milosz,

It still did not work actually when I run the form one row from
database becomes too rows in the datagrid. Still the add button does
not function. When I press it no row is created. What could be the
problem?

Thanks

Mar 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frosty | last post by:
Howto make datagrid enforce rules of xml schema? Created xml schema in the designer. Constraints created there using the following <xs:simpleType name="zipcode"><xs:restriction...
2
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is...
2
by: zambizzi | last post by:
....I can't seem to get my hands on a control I'm loading in an editable datagrid. Here's my datagrid control: <asp:datagrid id="GLRulesGrid" runat="server" autogeneratecolumns="False"...
1
by: Dan11 | last post by:
here's C sharp code: Start code... private void searchBtn_Click(object sender, System.EventArgs e) { //For sqlCommand1 is a search by Wkst ID (employeeID on pubs) params if...
3
by: dbuchanan | last post by:
Hello, (Windows forms - SQL Server) I fill my datagrid with a stored procedure that includes relationships to lookup tables so that users can see the values of the combobox selections rather...
4
by: cooltech77 | last post by:
Hi, I am trying to build the following functionality in the datagrid. I have a lot of columns in the datagrid which are being populated from the database and the user needs to scroll...
0
by: Ambica Jain | last post by:
I have a data grid called Files, which has some columns like FileName, Col1, Col2, ... , Col8. Then i have a combobox which allows user to select from Col1 to Col8 and based on this selection, i...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: rcoco | last post by:
Hi, I have a datagrid that is ment to insert data. But when I run the form only the header appears. I would like some advise from you all and solve this problem I'm using visual studio 2003. My...
8
by: Brock | last post by:
I am trying to populate a Crystal Report from data in my DataGrid. The reason for this is that I want the user to be able to change values without updating the database, but still have their report...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.