469,609 Members | 2,217 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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 3543
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Frosty | last post: by
2 posts views Thread by zambizzi | last post: by
4 posts views Thread by cooltech77 | last post: by
reply views Thread by Ambica Jain | last post: by
9 posts views Thread by rn5a | last post: by
reply views Thread by rcoco | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.