Datagrid fails to show data!

R

rcoco

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.DataGridCommandEventArgs 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();
}
 
G

Guest

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.
 
R

rcoco

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.DataGridCommandEventArgs 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.
 
G

Guest

You're are still operating on local variables in each function so you reasign
data source in Fill() method.
 
R

rcoco

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
 
G

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top