By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

Editable datagrid

P: n/a
Hi,
I'm working on a asp.net project with a datagrid. I want the user to
be adding data into Sql database using the datagrid. So I'm using a
ListBox to do this and edit update and delete. I placed listbox in
EditItemTemplate and label in ItemTemplate. but when I try now to edit
it does not bring the lisbox but i can read the grid with the sql
content. my code actually looks like this:

public void Edit_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}

public void Cancel_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = -1;
BindDataGrid();
}

public void Update_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.ListBox st=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[5].FindControl("lbachobj");
System.Web.UI.WebControls.ListBox st1=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[4].FindControl("lbobj");
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into Object,AchievedObject where
@Object,@AchievedObject";
myCommand.Parameters.Add(new
SqlParameter("@AchievedObject",SqlDbType.Text));
myCommand.Parameters["@AchievedObject"].Value=st.SelectedValue;
myCommand.Parameters.Add(new SqlParameter("@Object",SqlDbType.Text));
myCommand.Parameters["@Object"].Value=st1.SelectedValue;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgid.EditItemIndex=-1;
BindDataGrid();
}

Thanks.

Feb 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Feb 22, 2:36 pm, "rcoco" <nclau...@yahoo.cawrote:
Hi,
I'm working on a asp.net project with a datagrid. I want the user to
be adding data into Sql database using the datagrid. So I'm using a
ListBox to do this and edit update and delete. I placed listbox in
EditItemTemplate and label in ItemTemplate. but when I try now to edit
it does not bring the lisbox but i can read the grid with the sql
content. my code actually looks like this:

public void Edit_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();

}

public void Cancel_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = -1;
BindDataGrid();

}

public void Update_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.ListBox st=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[5].FindControl("lbachobj*");
System.Web.UI.WebControls.ListBox st1=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[4].FindControl("lbobj");
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into Object,AchievedObject where
@Object,@AchievedObject";
myCommand.Parameters.Add(new
SqlParameter("@AchievedObject",SqlDbType.Text));
myCommand.Parameters["@AchievedObject"].Value=st.SelectedValue;
myCommand.Parameters.Add(new SqlParameter("@Object",SqlDbType.Text));
myCommand.Parameters["@Object"].Value=st1.SelectedValue;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgid.EditItemIndex=-1;
BindDataGrid();

}

Thanks.
By data grid i assume you are talking about GridView. By default you
cannot insert new rows from within gridview however you can update and
delete. If you want to insert new rows than either go for the
DetailsView or FormView control. These controls by default give you
leverage to insert new records.

Feb 22 '07 #2

P: n/a
On Feb 22, 2:43 pm, qasim.m...@gmail.com wrote:
On Feb 22, 2:36 pm, "rcoco" <nclau...@yahoo.cawrote:


Hi,
I'm working on a asp.net project with a datagrid. I want the user to
be adding data into Sql database using the datagrid. So I'm using a
ListBox to do this and edit update and delete. I placed listbox in
EditItemTemplate and label in ItemTemplate. but when I try now to edit
it does not bring the lisbox but i can read the grid with the sql
content. my code actually looks like this:
public void Edit_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}
public void Cancel_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = -1;
BindDataGrid();
}
public void Update_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.ListBox st=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[5].FindControl("lbachobj**");
System.Web.UI.WebControls.ListBox st1=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[4].FindControl("lbobj");
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into Object,AchievedObject where
@Object,@AchievedObject";
myCommand.Parameters.Add(new
SqlParameter("@AchievedObject",SqlDbType.Text));
myCommand.Parameters["@AchievedObject"].Value=st.SelectedValue;
myCommand.Parameters.Add(new SqlParameter("@Object",SqlDbType.Text));
myCommand.Parameters["@Object"].Value=st1.SelectedValue;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgid.EditItemIndex=-1;
BindDataGrid();
}
Thanks.

By data grid i assume you are talking about GridView. By default you
cannot insert new rows from within gridview however you can update and
delete. If you want to insert new rows than either go for the
DetailsView or FormView control. These controls by default give you
leverage to insert new records.- Hide quoted text -

- Show quoted text -
Thanks,
If I have gotten you write edit cannot insert a value into database.
So how can I solve this problem? How do I get Detailview or formview?
Thanks

Feb 22 '07 #3

P: n/a
Detailsview and Formview are standard web server controls in ASP.NET 2.0.
For example try
<asp:DetailsView runat="server" ID = "someID"></asp:DetailsView>

Which .net version are you using, 1.x, 2.0, ...?

--
Regards,
Kees de Winter
"rcoco" <nc******@yahoo.cawrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...
On Feb 22, 2:43 pm, qasim.m...@gmail.com wrote:
On Feb 22, 2:36 pm, "rcoco" <nclau...@yahoo.cawrote:


Hi,
I'm working on a asp.net project with a datagrid. I want the user to
be adding data into Sql database using the datagrid. So I'm using a
ListBox to do this and edit update and delete. I placed listbox in
EditItemTemplate and label in ItemTemplate. but when I try now to edit
it does not bring the lisbox but i can read the grid with the sql
content. my code actually looks like this:
public void Edit_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}
public void Cancel_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = -1;
BindDataGrid();
}
public void Update_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.ListBox st=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[5].FindControl("lbachobj*
*");
System.Web.UI.WebControls.ListBox st1=new
System.Web.UI.WebControls.ListBox();
st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[4].FindControl("lbobj");
SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into Object,AchievedObject where
@Object,@AchievedObject";
myCommand.Parameters.Add(new
SqlParameter("@AchievedObject",SqlDbType.Text));
myCommand.Parameters["@AchievedObject"].Value=st.SelectedValue;
myCommand.Parameters.Add(new SqlParameter("@Object",SqlDbType.Text));
myCommand.Parameters["@Object"].Value=st1.SelectedValue;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgid.EditItemIndex=-1;
BindDataGrid();
}
Thanks.

By data grid i assume you are talking about GridView. By default you
cannot insert new rows from within gridview however you can update and
delete. If you want to insert new rows than either go for the
DetailsView or FormView control. These controls by default give you
leverage to insert new records.- Hide quoted text -

- Show quoted text -
Thanks,
If I have gotten you write edit cannot insert a value into database.
So how can I solve this problem? How do I get Detailview or formview?
Thanks
Feb 23 '07 #4

P: n/a
Hi kees,

I'm using 2.0. I'm actually no longer using a listbox but in stead I'm
using a textbox. But now i'm facing another challenge. The user has to
insert new data in SQL using a datagrid. So I'm trying using a button.
The button should be able to get a new row so that user could enter
new records and I created another button that will enable the user to
save the records into SQL. My problem is to get the row ready to
insert data. could you be knowing how I could go about this?
Thanks

On Feb 23, 2:59 pm, "Kees de Winter" <nos...@nospam.orgwrote:
Detailsview and Formview are standard web server controls in ASP.NET 2.0.
For example try
<asp:DetailsView runat="server" ID = "someID"></asp:DetailsView>

Which .net version are you using, 1.x, 2.0, ...?

--
Regards,
Kees de Winter

"rcoco" <nclau...@yahoo.cawrote in message

news:11**********************@k78g2000cwa.googlegr oups.com...
On Feb 22, 2:43 pm, qasim.m...@gmail.com wrote:
On Feb 22, 2:36 pm, "rcoco" <nclau...@yahoo.cawrote:
Hi,
I'm working on a asp.net project with a datagrid. I want the user to
be adding data into Sql database using the datagrid. So I'm using a
ListBox to do this and edit update and delete. I placed listbox in
EditItemTemplate and label in ItemTemplate. but when I try now to edit
it does not bring the lisbox but i can read the grid with the sql
content. my code actually looks like this:
public void Edit_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}
public void Cancel_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = -1;
BindDataGrid();
}
public void Update_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.ListBox st=new
System.Web.UI.WebControls.ListBox();

st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[5].FindControl("lbachobj**
*"); System.Web.UI.WebControls.ListBox st1=new
System.Web.UI.WebControls.ListBox();

st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[4].FindControl("lbobj");


SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into Object,AchievedObject where
@Object,@AchievedObject";
myCommand.Parameters.Add(new
SqlParameter("@AchievedObject",SqlDbType.Text));
myCommand.Parameters["@AchievedObject"].Value=st.SelectedValue;
myCommand.Parameters.Add(new SqlParameter("@Object",SqlDbType.Text));
myCommand.Parameters["@Object"].Value=st1.SelectedValue;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgid.EditItemIndex=-1;
BindDataGrid();
}
Thanks.
By data grid i assume you are talking about GridView. By default you
cannot insert new rows from within gridview however you can update and
delete. If you want to insert new rows than either go for the
DetailsView or FormView control. These controls by default give you
leverage to insert new records.- Hide quoted text -
- Show quoted text -

Thanks,
If I have gotten you write edit cannot insert a value into database.
So how can I solve this problem? How do I get Detailview or formview?
Thanks- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Feb 23 '07 #5

P: n/a
What kind of datagrid do you mean? Or do you mean GridView?

One way of doing it is to have a DetailsView control for the data-entry and
a GridView for showing a list of existing rows. Connect the DetailsView to
some datasource (for example Sql DataSource) and use the Property Editor to
set the DefaultMode property of the DetailsView control to "Insert". Then
create an InsertCommand in the SqlDataSource using the wizard. You will get
aspx code like this

<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataSourceID="SqlDataSource1"
DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="SomeID" HeaderText="SomeID"
InsertVisible="False"
ReadOnly="True" SortExpression="SomeID" />
<asp:BoundField DataField="SomeField1"
HeaderText="SomeField1" SortExpression="SomeField1" />
<asp:BoundField DataField="SomeField2"
HeaderText="SomeField2" SortExpression="SomeField2" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ your connectionstring %>"
SelectCommand="SELECT * FROM SomeTable"
InsertCommand="INSERT INTO SomeTable(somefield1, somefield2, )
VALUES (bla,bla,,)"></asp:SqlDataSource>

But there are lots of tutorials online for using the detailsview control
that explain this much better. Just search for 'detailsview insert'.

--
Regards,
Kees


"rcoco" <nc******@yahoo.cawrote in message
news:11*********************@p10g2000cwp.googlegro ups.com...
Hi kees,

I'm using 2.0. I'm actually no longer using a listbox but in stead I'm
using a textbox. But now i'm facing another challenge. The user has to
insert new data in SQL using a datagrid. So I'm trying using a button.
The button should be able to get a new row so that user could enter
new records and I created another button that will enable the user to
save the records into SQL. My problem is to get the row ready to
insert data. could you be knowing how I could go about this?
Thanks

On Feb 23, 2:59 pm, "Kees de Winter" <nos...@nospam.orgwrote:
Detailsview and Formview are standard web server controls in ASP.NET 2.0.
For example try
<asp:DetailsView runat="server" ID = "someID"></asp:DetailsView>

Which .net version are you using, 1.x, 2.0, ...?

--
Regards,
Kees de Winter

"rcoco" <nclau...@yahoo.cawrote in message

news:11**********************@k78g2000cwa.googlegr oups.com...
On Feb 22, 2:43 pm, qasim.m...@gmail.com wrote:
On Feb 22, 2:36 pm, "rcoco" <nclau...@yahoo.cawrote:
Hi,
I'm working on a asp.net project with a datagrid. I want the user to
be adding data into Sql database using the datagrid. So I'm using a
ListBox to do this and edit update and delete. I placed listbox in
EditItemTemplate and label in ItemTemplate. but when I try now to edit
it does not bring the lisbox but i can read the grid with the sql
content. my code actually looks like this:
public void Edit_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
}
public void Cancel_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgid.EditItemIndex = -1;
BindDataGrid();
}
public void Update_dgid(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
System.Web.UI.WebControls.ListBox st=new
System.Web.UI.WebControls.ListBox();

st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[5].FindControl("lbachobj*
*
*"); System.Web.UI.WebControls.ListBox st1=new
System.Web.UI.WebControls.ListBox();

st=(System.Web.UI.WebControls.ListBox)e.Item.Cells[4].FindControl("lbobj");
>

SqlCommand myCommand=new SqlCommand();
myCommand.Connection=con;
myCommand.CommandText="insert into Object,AchievedObject where
@Object,@AchievedObject";
myCommand.Parameters.Add(new
SqlParameter("@AchievedObject",SqlDbType.Text));
myCommand.Parameters["@AchievedObject"].Value=st.SelectedValue;
myCommand.Parameters.Add(new SqlParameter("@Object",SqlDbType.Text));
myCommand.Parameters["@Object"].Value=st1.SelectedValue;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
dgid.EditItemIndex=-1;
BindDataGrid();
}
Thanks.
By data grid i assume you are talking about GridView. By default you
cannot insert new rows from within gridview however you can update and
delete. If you want to insert new rows than either go for the
DetailsView or FormView control. These controls by default give you
leverage to insert new records.- Hide quoted text -
- Show quoted text -

Thanks,
If I have gotten you write edit cannot insert a value into database.
So how can I solve this problem? How do I get Detailview or formview?
Thanks- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -


Feb 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.