Monday, March 12, 2012

How to copy single row in datagrid

Hi to all,

I would like to copy data (whole single row at once) from web-based datagrid to off line Excel spreadsheet. I am using Vb.net and Access database.

Please help me!

Regards

Harsh

Here is an example that uses the Northwind database as an example. It creates a CSV file that Excel can open directly, so there is no need to start interfacing with the COM Excel DLL. If your text contains commas, you can use another seperator such as vbTab. Hope this helps

<%@.PageLanguage="VB" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">

ProtectedSub Gridview1_RowCommand(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.GridViewCommandEventArgs)

If e.CommandName ="Export"Then

' Create an instance of StreamWriter to write text to a file.

Using swAs System.IO.StreamWriter =New System.IO.StreamWriter(MapPath("App_Data/TestFile.csv"))

Dim rAs GridViewRow = Gridview1.Rows(CInt(e.CommandArgument))

Dim csvLineAsString =String.Empty

For indexAsInteger = 0To r.Cells.Count - 1

If csvLine =""Then

csvLine = r.Cells(index).Text

Else

csvLine +="," + r.Cells(index).Text

EndIf

Next

sw.WriteLine(csvLine)

sw.Close()

EndUsing

EndIf

EndSub

</script>

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<asp:GridViewID="Gridview1"runat="server"AutoGenerateColumns="False"DataKeyNames="CustomerID"

DataSourceID="SqlDataSource1"OnRowCommand="Gridview1_RowCommand">

<Columns>

<asp:TemplateFieldHeaderText="Click link to Export">

<ItemTemplate>

<asp:LinkButtonID="LinkButton1"runat="server"CommandName="Export"CommandArgument='<%# Container.DataItemIndex %>'>Export</asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundFieldDataField="CustomerID"HeaderText="CustomerID"ReadOnly="True"SortExpression="CustomerID"/>

<asp:BoundFieldDataField="CompanyName"HeaderText="CompanyName"SortExpression="CompanyName"/>

<asp:BoundFieldDataField="PostalCode"HeaderText="PostalCode"SortExpression="PostalCode"/>

<asp:BoundFieldDataField="Phone"HeaderText="Phone"SortExpression="Phone"/>

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT [CustomerID], [CompanyName], [PostalCode], [Phone] FROM [Customers]">

</asp:SqlDataSource>

</form>

</body>

</html>


Thanks sbyard for the code. I appreciate you. I will try this code and hope it will help me.

Regards

Harsh

Labels: , , , , , , , , , ,

How to copy DataRows between DataTables?

Hi all,
I want to copy or move an existing DataRow wich belongs to a DataTable to
another DataTable, but I got the exception "This row already belongs to
another table".
I tried to use the ImportRow method, but this method inserts the row at the
end of the DataTable, and I need to put it at an specific position. The
method InsertAt returns the expeption ahead.
There's a way to copy a DataRow in a specific position? Or, maybe, a way to
detach the row from the original DataTable?
Thanks in advance,
Fabrício de Novaes Kucinskis.Well, I found a solution, copying the source row data to a new row, using
the ItemArray property, and then attaching the new row to the destination
table via InsertAt.
The problem now is that the row is attached to the end of the DataTable, not
in the position I set in InsertAt. My DataTable has a binded DataGrid.
I search the web, and it appears to be a .NET bug. Is it correct? If no,
what can I do? If yes, WHAT CAN I DO'?
Thanks again,
Fabrício.
"Fabrício de Novaes Kucinskis" <fabricio.nk@.uol.com.br> wrote in message
news:eF3NfRvPDHA.2480@.tk2msftngp13.phx.gbl...
> Hi all,
> I want to copy or move an existing DataRow wich belongs to a DataTable to
> another DataTable, but I got the exception "This row already belongs to
> another table".
> I tried to use the ImportRow method, but this method inserts the row at
the
> end of the DataTable, and I need to put it at an specific position. The
> method InsertAt returns the expeption ahead.
> There's a way to copy a DataRow in a specific position? Or, maybe, a way
to
> detach the row from the original DataTable?
> Thanks in advance,
>
> Fabrício de Novaes Kucinskis.
>
I am unsure if this is a bug or not. How you may get around this is use a sorted DataView on the DataTable and bind your grid to the DataView.
>--Original Message--
>Well, I found a solution, copying the source row data to a new row, using
>the ItemArray property, and then attaching the new row to the destination
>table via InsertAt.
>The problem now is that the row is attached to the end of the DataTable, not
>in the position I set in InsertAt. My DataTable has a binded DataGrid.
>I search the web, and it appears to be a .NET bug. Is it correct? If no,
>what can I do? If yes, WHAT CAN I DO'?
>Thanks again,
>
>Fabr=EDcio.
>
>"Fabr=EDcio de Novaes Kucinskis" <fabricio.nk@.uol.com.br> wrote in message
>news:eF3NfRvPDHA.2480@.tk2msftngp13.phx.gbl...
>> Hi all,
>> I want to copy or move an existing DataRow wich belongs to a DataTable to
>> another DataTable, but I got the exception "This row already belongs to
>> another table".
>> I tried to use the ImportRow method, but this method inserts the row at
>the
>> end of the DataTable, and I need to put it at an specific position. The
>> method InsertAt returns the expeption ahead.
>> There's a way to copy a DataRow in a specific position? Or, maybe, a way
>to
>> detach the row from the original DataTable?
>> Thanks in advance,
>>
>> Fabr=EDcio de Novaes Kucinskis.
>>
>
>.
>
Hi, David. Thanks for your answer.
I cannot use a DataView because the user wants to edit the data in DataGrid.
"David Brenchley" <brenchld@.iomega.com> wrote in message
news:052e01c33f50$e8771e00$a101280a@.phx.gbl...
I am unsure if this is a bug or not. How you may get
around this is use a sorted DataView on the DataTable and
bind your grid to the DataView.
>--Original Message--
>Well, I found a solution, copying the source row data to
a new row, using
>the ItemArray property, and then attaching the new row
to the destination
>table via InsertAt.
>The problem now is that the row is attached to the end
of the DataTable, not
>in the position I set in InsertAt. My DataTable has a
binded DataGrid.
>I search the web, and it appears to be a .NET bug. Is it
correct? If no,
>what can I do? If yes, WHAT CAN I DO'?
>Thanks again,
>
>Fabrício.
>
>"Fabrício de Novaes Kucinskis" <fabricio.nk@.uol.com.br>
wrote in message
>news:eF3NfRvPDHA.2480@.tk2msftngp13.phx.gbl...
>> Hi all,
>> I want to copy or move an existing DataRow wich
belongs to a DataTable to
>> another DataTable, but I got the exception "This row
already belongs to
>> another table".
>> I tried to use the ImportRow method, but this method
inserts the row at
>the
>> end of the DataTable, and I need to put it at an
specific position. The
>> method InsertAt returns the expeption ahead.
>> There's a way to copy a DataRow in a specific
position? Or, maybe, a way
>to
>> detach the row from the original DataTable?
>> Thanks in advance,
>>
>> Fabrício de Novaes Kucinskis.
>>
>
>.
>
>I cannot use a DataView because the user wants to edit the data in DataGrid.
And why do you think you can't edit your data anymore, if you use a
DataView' You still can!
Check it out - every DataTable has a "DefaultView" property, which
exposes the default view of the table - you can use that to set the
Sort property of the view, and then use the view as your grid's
source, instead of your table directly.
Read up on ADO.NET in a good book - it has *LOTS* of goodies to offer!
DataView also allow you to specify e.g. a RowFIlter - very powerful
stuff. And everything stays editable!
Marc
================================================================Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Oh my God. I don't believe I lost a lot of time, based on this supposition.
Marc, thanks for your answer. I'll try your tips.
"Marc Scheuner [MVP ADSI]" <m.scheuner@.inova.SPAMBEGONE.ch> wrote in message
news:c5h7gvkjkoq9ap4m9t3pdr1h41gbr9bp0h@.4ax.com...
> >I cannot use a DataView because the user wants to edit the data in
DataGrid.
> And why do you think you can't edit your data anymore, if you use a
> DataView' You still can!
> Check it out - every DataTable has a "DefaultView" property, which
> exposes the default view of the table - you can use that to set the
> Sort property of the view, and then use the view as your grid's
> source, instead of your table directly.
> Read up on ADO.NET in a good book - it has *LOTS* of goodies to offer!
> DataView also allow you to specify e.g. a RowFIlter - very powerful
> stuff. And everything stays editable!
> Marc
> ================================================================> Marc Scheuner May The Source Be With You!
> Bern, Switzerland m.scheuner(at)inova.ch

Labels: , , , , , , , , , , ,

How to copy a row in a Dataview based on user selection?

I have a datatable that I'm displaying in a Winform Datagrid via a
DataView, and I want the user to be able to right-click on any of the rows
and select a "copy row" command from a context menu. Then I need to make
the copy happen.
I've got the context menu up and responding appropriately, but I'm having
difficulty dealing with the copying:
Firstly, how do I identify what row the user wants to copy?
Second, how do I make a copy of the row? There is no "Clone" method for
a row. Do I need to do an element-by-element copy of the row? And if so,
can I do this with an enumerator or must I use column names?
I've been struggling with this for a half a day; I'd sure appreciate any
help.
--
MichaelMichael,
To find the current dataRow try
DataView dataView = (DataView)((CurrencyManager)
dataGrid1.BindingContext[ dataGrid1.DataSource, dataGrid1.DataMember
]).List;
DataRow dataRow = dataView[ dataGrid1.CurrentRowIndex ].Row;
To copy the row into another dataTable (with the same structure) try
destinationTable.Rows.Add(dataRow.ItemArray);
All the Best,
Phil.
"Michael Kellogg" wrote:
> I have a datatable that I'm displaying in a Winform Datagrid via a
> DataView, and I want the user to be able to right-click on any of the rows
> and select a "copy row" command from a context menu. Then I need to make
> the copy happen.
> I've got the context menu up and responding appropriately, but I'm having
> difficulty dealing with the copying:
> Firstly, how do I identify what row the user wants to copy?
> Second, how do I make a copy of the row? There is no "Clone" method for
> a row. Do I need to do an element-by-element copy of the row? And if so,
> can I do this with an enumerator or must I use column names?
>
> I've been struggling with this for a half a day; I'd sure appreciate any
> help.
> --
> Michael
>
<PhilWilliams@.discussions.microsoft.com> wrote:
> Michael,
> To find the current dataRow try
> DataView dataView = (DataView)((CurrencyManager)
dataGrid1.BindingContext [dataGrid1.DataSource,
dataGrid1.DataMember]).List;
> DataRow dataRow = dataView[ dataGrid1.CurrentRowIndex ].Row;
> To copy the row into another dataTable (with the same structure) try
> destinationTable.Rows.Add(dataRow.ItemArray);
> All the Best,
> Phil.
Thanks for the help, Phil. I'm still trying to understand exactly what
you're doing, here. I've never really understood BindingManagerBase
objects and their function. I like the way you added a copy of a row
without really cloning it; I would not have thought of that.
One issue I'm having is that, when capturing the right-click that brings
up the context menu, if you click on the row itself (the technical term
escapes me; the little box on the left side of a row), the
CurrentRowIndex property does NOT seem to update with that row. You have
to actually click on a cell inside the row. Consequently, the row
returned is wrong. I tried doing a SELECT on the row identified in the
MouseDown event handler (where the use of HitTest will give me the
correct row index), but this doesn't seem to work, either. Is there a
way to "activate" a row so the CurrentRowIndex property is guaranteed to
be right?
Thanks again for the help.
MK
Michael,
In contextMenu_Popup, try
DataGrid.HitTestInfo hti = dataGrid1.HitTest(
dataGrid1.PointToClient(Control.MousePosition));
if (hti.Row != -1)
{
DataView dataView = (DataView)((CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember ]).List;
DataRow dataRow = dataView[ hti.Row ].Row;
}
Cheers,
Phil.
"Michael Kellogg" wrote:
> <PhilWilliams@.discussions.microsoft.com> wrote:
> > Michael,
> > To find the current dataRow try
> >
> > DataView dataView = (DataView)((CurrencyManager)
> dataGrid1.BindingContext [dataGrid1.DataSource,
> dataGrid1.DataMember]).List;
> > DataRow dataRow = dataView[ dataGrid1.CurrentRowIndex ].Row;
> >
> > To copy the row into another dataTable (with the same structure) try
> > destinationTable.Rows.Add(dataRow.ItemArray);
> >
> > All the Best,
> > Phil.
>
> Thanks for the help, Phil. I'm still trying to understand exactly what
> you're doing, here. I've never really understood BindingManagerBase
> objects and their function. I like the way you added a copy of a row
> without really cloning it; I would not have thought of that.
> One issue I'm having is that, when capturing the right-click that brings
> up the context menu, if you click on the row itself (the technical term
> escapes me; the little box on the left side of a row), the
> CurrentRowIndex property does NOT seem to update with that row. You have
> to actually click on a cell inside the row. Consequently, the row
> returned is wrong. I tried doing a SELECT on the row identified in the
> MouseDown event handler (where the use of HitTest will give me the
> correct row index), but this doesn't seem to work, either. Is there a
> way to "activate" a row so the CurrentRowIndex property is guaranteed to
> be right?
> Thanks again for the help.
> MK
>
"=?Utf-8?B?UGhpbCBXaWxsaWFtcw==?="
<PhilWilliams@.discussions.microsoft.com> wrote:
> Michael,
> In contextMenu_Popup, try
> DataGrid.HitTestInfo hti = dataGrid1.HitTest(
> dataGrid1.PointToClient(Control.MousePosition));
> if (hti.Row != -1)
> {
> DataView dataView => (DataView)((CurrencyManager)dataGrid1.BindingContext[
> dataGrid1.DataSource, dataGrid1.DataMember ]).List;
> DataRow dataRow = dataView[ hti.Row ].Row;
> }
That is very clever; I like it. Any chance I can do something like this
in the MenuItem CLICK handler instead (obviously the mouse will have
moved by the time I select a context menu item)? I can get the row using
your method either in the POPUP, as you suggest, or in the datagrid CLICK
handler, as well. Then my thought was to stash the row number in a
member variable and use it in the MenuItem CLICK handler. However, this
didn't seem so elegant to me and I thought it'd be nice to actually
determine the row inside the handler. Am I obsessing?
--
Michael
Michael,
Add the following lines
private int clickedRow = -1;
.
.
.
private void contextMenu_Popup(object sender, System.EventArgs e)
{
DataGrid.HitTestInfo hti = dataGrid1.HitTest(
dataGrid1.PointToClient(Control.MousePosition));
clickedRow = hti.Row;
menuItemCopyRow.Enabled = clickedRow != -1;
}
private void menuItemCopyRow_Click(object sender, System.EventArgs e)
{
if (clickedRow == -1) return;
DataView dataView = (DataView)((CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember ]).List;
DataRow dataRow = dataView[ clickedRow ].Row;
}
All the best,
Phil.
> >
> > if (hti.Row != -1)
"Michael Kellogg" wrote:
> "=?Utf-8?B?UGhpbCBXaWxsaWFtcw==?="
> <PhilWilliams@.discussions.microsoft.com> wrote:
> > Michael,
> > In contextMenu_Popup, try
> >
> > DataGrid.HitTestInfo hti = dataGrid1.HitTest(
> > dataGrid1.PointToClient(Control.MousePosition));
> >
> > if (hti.Row != -1)
> > {
> > DataView dataView => > (DataView)((CurrencyManager)dataGrid1.BindingContext[
> > dataGrid1.DataSource, dataGrid1.DataMember ]).List;
> > DataRow dataRow = dataView[ hti.Row ].Row;
> > }
> That is very clever; I like it. Any chance I can do something like this
> in the MenuItem CLICK handler instead (obviously the mouse will have
> moved by the time I select a context menu item)? I can get the row using
> your method either in the POPUP, as you suggest, or in the datagrid CLICK
> handler, as well. Then my thought was to stash the row number in a
> member variable and use it in the MenuItem CLICK handler. However, this
> didn't seem so elegant to me and I thought it'd be nice to actually
> determine the row inside the handler. Am I obsessing?
> --
> Michael
>
<PhilWilliams@.discussions.microsoft.com> wrote:
> Michael,
> Add the following lines
> private int clickedRow = -1;
> .
> .
> .
> private void contextMenu_Popup(object sender, System.EventArgs e)
> {
> DataGrid.HitTestInfo hti = dataGrid1.HitTest(
> dataGrid1.PointToClient(Control.MousePosition));
> clickedRow = hti.Row;
> menuItemCopyRow.Enabled = clickedRow != -1;
> }
> private void menuItemCopyRow_Click(object sender, System.EventArgs e)
> {
> if (clickedRow == -1) return;
> DataView dataView => (DataView)((CurrencyManager)dataGrid1.BindingContext[
> dataGrid1.DataSource, dataGrid1.DataMember ]).List;
> DataRow dataRow = dataView[ clickedRow ].Row;
> }
> All the best,
> Phil.
Thanks, Phil!
-mk

Labels: , , , , , , , , , , , , , ,

How to copy a row from one DataTable to another DataTable?

Hi all,
Assume that both DataTable are the same schema, I want to copy a row from
one DataTable to another DataTable by
m_dtItem.Rows.Add(m_dtOrgItem.Rows(0))
However, it said that
Exception Details: System.ArgumentException: This row already belongs to
another table.
So, How can copy a row from one DataTable to another DataTable?
Thank you.use m_dtItem.ImportRow instead of m_dtItem.Rows.Add
"angus" <angus@.angus.com> wrote in message
news:ODVqYLWwEHA.1292@.TK2MSFTNGP10.phx.gbl...
> Hi all,
> Assume that both DataTable are the same schema, I want to copy a row from
> one DataTable to another DataTable by
> m_dtItem.Rows.Add(m_dtOrgItem.Rows(0))
> However, it said that
> Exception Details: System.ArgumentException: This row already belongs to
> another table.
> So, How can copy a row from one DataTable to another DataTable?
> Thank you.
>
Hi -
You have to use the ItemArray to copy the row
m_dtItem.Rows.Add(m_dtOrgItem.Rows(0).ItemArray)
Shankar
11/3/2004 10:36:04 AM
angus <angus@.angus.com> wrote in message
<ODVqYLWwEHA.1292@.TK2MSFTNGP10.phx.gbl>
> Hi all,
> Assume that both DataTable are the same schema, I want to copy a
row from
> one DataTable to another DataTable by
> m_dtItem.Rows.Add(m_dtOrgItem.Rows(0))
> However, it said that
> Exception Details: System.ArgumentException: This row already
belongs to
> another table.
> So, How can copy a row from one DataTable to another DataTable?
> Thank you.
Right
To prevent that everybody reads everything again and againg
:-)
> use m_dtItem.ImportRow instead of m_dtItem.Rows.Add
>

Labels: , , , , , ,

how to convert a row in gridview to my business class DialUpRow generated by dataSet desig

I have GridView with: objectdatasource="objectdatasource1"

Objectdatasource1 is linked to my BLL (business layer with a dataset designer genrating: DialUpRow as an item of my datatable: DialupUpDataTable

I want to convert the selected row to my class: DialUpRow

I get the selected row using: myGRidView.SelectedIndex

But I don t know how to convert the grid row to my DialUpRow in order to get intellisence of my row properies (calss properties in code behind)

DataItem property of the GridViewRow would give the dataitem bound to the GridView. Below is a sample code. In the code, I haven't done any error checking, please ensure you do in your code.

Dim row as GridViewRow = GridView1.Rows(GridView1.SelectedIndex)if row isnot nothing thenDim drow as DialUpRow =CType(row.DataItem, DialUpRow)endif

Labels: , , , , , , , , , , , , , , , , ,