Showing posts with label gridview. Show all posts
Showing posts with label gridview. Show all posts

Saturday, March 24, 2012

Populate GridView with DataSet

I was using a DataView to bind records from a DB table to a DataGrid
using the following code:
Dim sqlDapter As SqlDataAdapter
Dim dSet As DataSet
Dim dView As DataView
sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
dSet = New DataSet()
dView = New DataView
sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView
dgUsers.DataSource = dView
dgUsers.DataBind()
The above works fine but I did like to bind the DataGrid to a GridView
instead of a DataView as the above code shows. What I did is deleted
the DataView from the above code & added a GridView i.e. all the
instances of the DataView were replaced with GridView i.e. changed the
variable name 'dView' to 'gView' but I get this error:
Value of type 'System.Data.DataView' cannot be converted to
'System.Web.UI.WebControls.GridView'
pointing to this line
gView = dSet.Tables("Users").DefaultView
How do I populate the GridView with the DataSet?Your DataSet and DataView variables stay the same. You will simply set
the DataSource of the GridView to your existing dView variable.
Using what you started with...

> sqlDapter.Fill(dSet, "Users")
> dView = dSet.Tables("Users").DefaultView

> dgUsers.DataSource = dView
> dgUsers.DataBind()
Assuming the GridView is named gvUsers, make the last two lines...
gvUsers.DataSource = dView
gvUsers.DataBind()
The DataGrid and GridView both take a DataView as the DataSource.
Brennan Stehling
http://brennan.offwhite.net/blog/
rn5a@.rediffmail.com wrote:
> I was using a DataView to bind records from a DB table to a DataGrid
> using the following code:
> Dim sqlDapter As SqlDataAdapter
> Dim dSet As DataSet
> Dim dView As DataView
> sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
> dSet = New DataSet()
> dView = New DataView
> sqlDapter.Fill(dSet, "Users")
> dView = dSet.Tables("Users").DefaultView
> dgUsers.DataSource = dView
> dgUsers.DataBind()
> The above works fine but I did like to bind the DataGrid to a GridView
> instead of a DataView as the above code shows. What I did is deleted
> the DataView from the above code & added a GridView i.e. all the
> instances of the DataView were replaced with GridView i.e. changed the
> variable name 'dView' to 'gView' but I get this error:
> Value of type 'System.Data.DataView' cannot be converted to
> 'System.Web.UI.WebControls.GridView'
> pointing to this line
> gView = dSet.Tables("Users").DefaultView
> How do I populate the GridView with the DataSet?

Populate GridView with DataSet

I was using a DataView to bind records from a DB table to a DataGrid
using the following code:

Dim sqlDapter As SqlDataAdapter
Dim dSet As DataSet
Dim dView As DataView

sqlDapter = New SqlDataAdapter(strSQL, sqlConn)

dSet = New DataSet()
dView = New DataView

sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView

dgUsers.DataSource = dView
dgUsers.DataBind()

The above works fine but I did like to bind the DataGrid to a GridView
instead of a DataView as the above code shows. What I did is deleted
the DataView from the above code & added a GridView i.e. all the
instances of the DataView were replaced with GridView i.e. changed the
variable name 'dView' to 'gView' but I get this error:

Value of type 'System.Data.DataView' cannot be converted to
'System.Web.UI.WebControls.GridView'

pointing to this line

gView = dSet.Tables("Users").DefaultView

How do I populate the GridView with the DataSet?Your DataSet and DataView variables stay the same. You will simply set
the DataSource of the GridView to your existing dView variable.

Using what you started with...

Quote:

Originally Posted by

sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView


Quote:

Originally Posted by

dgUsers.DataSource = dView
dgUsers.DataBind()


Assuming the GridView is named gvUsers, make the last two lines...

gvUsers.DataSource = dView
gvUsers.DataBind()

The DataGrid and GridView both take a DataView as the DataSource.

Brennan Stehling
http://brennan.offwhite.net/blog/
rn5a@.rediffmail.com wrote:

Quote:

Originally Posted by

I was using a DataView to bind records from a DB table to a DataGrid
using the following code:
>
Dim sqlDapter As SqlDataAdapter
Dim dSet As DataSet
Dim dView As DataView
>
sqlDapter = New SqlDataAdapter(strSQL, sqlConn)
>
dSet = New DataSet()
dView = New DataView
>
sqlDapter.Fill(dSet, "Users")
dView = dSet.Tables("Users").DefaultView
>
dgUsers.DataSource = dView
dgUsers.DataBind()
>
The above works fine but I did like to bind the DataGrid to a GridView
instead of a DataView as the above code shows. What I did is deleted
the DataView from the above code & added a GridView i.e. all the
instances of the DataView were replaced with GridView i.e. changed the
variable name 'dView' to 'gView' but I get this error:
>
Value of type 'System.Data.DataView' cannot be converted to
'System.Web.UI.WebControls.GridView'
>
pointing to this line
>
gView = dSet.Tables("Users").DefaultView
>
How do I populate the GridView with the DataSet?

Populate insert form field from GridView selection

Hi,

I'm trying to do something that seems like it should be simple, but I'm having trouble getting it to work. I have a page with a GridView and a FormView in Insert mode.

When the user selects a row in the gridview, I want to take the data keys from that selection and populate them to the appropriate textboxes in the form. I'm trying to do this in the

"GridView1_SelectedIndexChanged event" with the following code:

TextBox t;

t = (TextBox)FormView1.FindControl("myTextBox");

t.Text = (String)GridView1.SelectedDataKey.Values[0];

The text in "myTextBox" doesn't change, it remains blank. I've tried to find another event in which to put this code but so far no luck. I'm a newbie so I'm sure it's something obvious.

Can anyone help me with this? Thanks in advance.

Try moving your code to the FormView.DataBound event.


Hi Ed,

When I do that, I get "Object reference not set to an instance of an object. " I'm guessing that this is because the formview1_databound fires when the page is first loading, before there is a selected key from the gridview. I'm not sure that this is the case, it's just my best guess.

Do you have any suggestion for a workaround or alternate approach?

Thanks.


Once try this

ProtectedSub GridView1_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles GridView1.SelectedIndexChanged

Formview1.PageIndex = GridView1.SelectedIndex

EndSub


Hi Mahesh,

Okay, now I have this code but I still don't see the text I want populated into the text box. What am I doing wrong?

protectedvoid GridView1_SelectedIndexChanged(object sender,EventArgs e)

{

FormView1.PageIndex = GridView1.SelectedIndex;

TextBox t;

t = (TextBox)FormView1.FindControl("myTextBox");

t.Text = (String)GridView1.SelectedDataKey.Values[0];

}


How about this:


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.SelectedIndex == i)
{
TextBox t;

t = (TextBox)FormView1.FindControl("myTextBox");
t.Text = (String)GridView1.SelectedDataKey.Values[0];
}
}
}


Within your FormView.DataBound event, simply place a conditional statement which checks for the existence of a SelectedItem.

if (!GridView1.SelectedIndex.Equals(-1))


That did it! Thanks very much (I told you that I was a newbie :-).

Friday, March 16, 2012

Populating a GridView in code

I am looking for examples for populating a GridView in code.
I have a GridView that I want to show unbound when the form opens and then
filled with different collections of data based on options the user selects.
Any help is greatly appreciated.The idea is that based on the user selection, you would databind the grid to
different datasources (collections). You better ask specific questions as
you go.
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Greg Smith" <gjs@.umn.edu> wrote in message
news:%23WddSY2$GHA.3560@.TK2MSFTNGP03.phx.gbl...
>I am looking for examples for populating a GridView in code.
> I have a GridView that I want to show unbound when the form opens and then
> filled with different collections of data based on options the user
> selects.
>
> Any help is greatly appreciated.
>

Populating a GridView in code

I am looking for examples for populating a GridView in code.

I have a GridView that I want to show unbound when the form opens and then
filled with different collections of data based on options the user selects.

Any help is greatly appreciated.The idea is that based on the user selection, you would databind the grid to
different datasources (collections). You better ask specific questions as
you go.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"Greg Smith" <gjs@.umn.eduwrote in message
news:%23WddSY2$GHA.3560@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>I am looking for examples for populating a GridView in code.
>
I have a GridView that I want to show unbound when the form opens and then
filled with different collections of data based on options the user
selects.
>
>
Any help is greatly appreciated.
>

Populating a label (inside InsertItemTemplate) with default value

Hello everyone,

I have one GridView and two different DetailsView's on ONE SINGLE page. I'm attempting to populate a label (which is in a DetailsView and also inside an InsertItemTemplate) with a value from the GridView. Of course I have datasources (<asp:SqlDataSource>) which are feeding the GridView and two DetailsViews. I also have all the commands with the datasources (SelectCommand, InsertCommand, UpdateCommand, etc.)

The reason why I have the ONE GridView and TWO DetailsViews is because I'm working with THREE separate SQL tables.

Here's my code:

The GridView (which contains the value inside a LinkButton)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Tracker_Number" DataSourceID="TOASearchResults" Width="498px" AllowPaging="True" AllowSorting="True">

<asp:LinkButton ID="LinkButton2" CommandName="Select" Runat="Server" OnClick="ShowEdit" Text='<%# Eval("Tracker_Number") %>'/>

The DetailsView (which contains the label I want to populate)

<asp:TemplateField HeaderText="Tracker Number" SortExpression="Tracker_Number">
<EditItemTemplate>
<asp:label ID="TrackerNumber" runat="server" Text='<%# Bind("Tracker_Number") %>'></asp:label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:label ID="lblTrackerNumber" runat="server"></asp:label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="TrackerNumber" runat="server" Text='<%# Eval("Tracker_Number") %>'></asp:label>
</ItemTemplate>
</asp:TemplateField>

I basically took this project over, so this isn't even my code. However, everything works except for this little part.

Any help would be appreciated,
Thanks!
-WesSo what's populating your DetailsView? The same data source? If everything else seems to be working, I think you should step through your code to see if those statements in the DetailsView are being called. (Too bad you use inline ASP.NET! ha ha!)
I got it!

I basically passed the value of the LinkButton (which was whatever the user clicked), stored it into a label and then assigned the value of that label to the label inside my InsertItemTemplate.

Thanks for the help anyway!