Please help

P

pargat singh

Hi Everyone,

I am new to asp.net and i want to develop small application.

I have three control on form (Listbox,button and Datagrid ).

I want to do the following.

1. When i clieck on row on datagrid i want this row to be highlighted.
2. when i select some value on ListBox and press button(>>) i want
that value to be added in grid under the selected (highlighted) row.
Values in the datagrid and ListBox are same like ClientID,ClientName.

Can anyone please tell me how can i do that.
Any help is much appriciate.

Thanks,

Pargat Gill
 
L

locitt

First, you should handles OnSelectedIndex event of DataGrid, and stores the
selectedindex in Session. Then when click on the button, retrieve the
SelectedIndex from Session and update to the current selected row.

But remember to store your datasource (DataTable or DataSet) that uses to
bind to the grid.

To select a row on click anywhere on datagrid item, you need to add a
TemplateColumn and in ItemTemplate, add a button which has CommandName is
Select. For instance, i named is btnSelectRow and it is a linkbutton

private void OnItemDataBound(...)
{
if(e.Item.ItemType == ListItemType.Item ||e.Item.ItemType ==
ListItemType.AlternatingItem)
{
LinkButton b =
(LinkButton)e.Item.Cells[0].FindControl("btnSelectRow");
e.Item.Atttributes.Add("onclick",
this.Page.GetPostBackClientEvent(b, "");
}
}

private void BindData()
{
SqlDataAdapter da = new SqlDataAdapter();
...

DataTable ds = new DataTable("Name");
ds.Fill(ds, "Name");
DataGrid1.DataSource = ds.DefaultView;
DataGrid1.DataBind();

Session["MyDataSource"] = dt;
}

private void OnButtonClick()
{
DataTable dt = Session["MyDataSource"];
dt.Rows[DataGrid1.SelectedIndex].ItemArray.SetValue(0,
listbox.SelectedValue)
//...set more for other columns if you want.

DataGrid1.DataSource = ds.DefaultView;
DataGrid1.DataBind();
}

....And set the DataGrid1.AllowPaging = false; to avoid mismatch the index
number between datagrid and your datatable.

Hope this help.

Here are some useful links
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp

http://www.datagridgirl.com/articles.aspx
...

Cheers.
locitt.
 
L

locitt

the LastColoredIndex should be Integer type is enough. If you want only
select a row in grid at a time, use theGrid.SelectedIndex.

And you dont have to use Session object and any other variables neither.

I attach for you an example and you can cache the event SelectedIndex if you
want. Sorry, I use C# not VB.

Cheers.

locitt.

----- Original Message -----
From: "Singh" <[email protected]>
To: <[email protected]>
Sent: Thursday, February 05, 2004 4:09 AM
Subject: Re: Please help

Thanks for your kind help.

I added a push button and OnItemCommand="HighLightME"

and on page behind i have
Public LastColoredIndex As
System.Web.UI.WebControls.DataGridCommandEventArgs


Sub HighLightME(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles DataGrid1.ItemCommand

If Session("test") = 1 Then
Session("oObj").Item.BackColor =
System.Drawing.Color.Red
End If
e.Item.BackColor =
System.Drawing.Color.Green
LastColoredIndex = e
Session("oObj") = LastColoredIndex
Session("test") = "1"

End Sub

Only problem with this is when i select (press the
button)first row it change that row but when i select
second row,first row still green and second row is
green .

I want that when i select second row first row should
change back to original color and sec. one should be
green.

Do you have any idea how can i do it.

Thanks,














Hi Everyone,

I am new to asp.net and i want to develop small
application.

I have three control on form (Listbox,button and
Datagrid ).

I want to do the following.

1. When i clieck on row on datagrid i want this row to
be highlighted.
2. when i select some value on ListBox and press
button(>>) i want
that value to be added in grid under the selected
(highlighted) row.
Values in the datagrid and ListBox are same like
ClientID,ClientName.

Can anyone please tell me how can i do that.
Any help is much appriciate.

Thanks,

Pargat Gill
Post a follow-up to this message

Message 2 in thread
From: locitt ([email protected])
Subject: Re: Please help


View this article only
Newsgroups:
microsoft.public.dotnet.framework.aspnet.datagridcontrol
Date: 2004-02-03 16:12:13 PST


First, you should handles OnSelectedIndex event of
DataGrid, and stores the
selectedindex in Session. Then when click on the
button, retrieve the
SelectedIndex from Session and update to the current
selected row.

But remember to store your datasource (DataTable or
DataSet) that uses to
bind to the grid.

To select a row on click anywhere on datagrid item,
you need to add a
TemplateColumn and in ItemTemplate, add a button which
has CommandName is
Select. For instance, i named is btnSelectRow and it
is a linkbutton

private void OnItemDataBound(...)
{
if(e.Item.ItemType == ListItemType.Item
||e.Item.ItemType ==
ListItemType.AlternatingItem)
{
LinkButton b =
(LinkButton)e.Item.Cells[0].FindControl("btnSelectRow");
e.Item.Atttributes.Add("onclick",
this.Page.GetPostBackClientEvent(b, "");
}
}

private void BindData()
{
SqlDataAdapter da = new SqlDataAdapter();
...

DataTable ds = new DataTable("Name");
ds.Fill(ds, "Name");
DataGrid1.DataSource = ds.DefaultView;
DataGrid1.DataBind();

Session["MyDataSource"] = dt;
}

private void OnButtonClick()
{
DataTable dt = Session["MyDataSource"];

dt.Rows[DataGrid1.SelectedIndex].ItemArray.SetValue(0,
listbox.SelectedValue)
//...set more for other columns if you want.

DataGrid1.DataSource = ds.DefaultView;
DataGrid1.DataBind();
}

...And set the DataGrid1.AllowPaging = false; to avoid
mismatch the index
number between datagrid and your datatable.

Hope this help.

Here are some useful links
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp

http://www.datagridgirl.com/articles.aspx
..

Cheers.
locitt.




__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/
 

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

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top