Fill DataGrid

  • Thread starter Thread starter ruca
  • Start date Start date
R

ruca

Hi,

How can I fill a datagrid without accessing to a database or any type of
file, only filling with a command button like this:
I have 3 textboxes that I must fill, and a button that when clicked pass
text of this 3 textboxes to my datagrid.

I want to manage (edit , update, etc) a datagrid but without accessing
DataBase. The unique interaction with that DB is a button that save all that
I have in datagrid into DB.

Hope you understand.
 
Hi Ruca,

Please tell that you are using a WebDatagrid when you ask this in the
language.VB newsgroup.

I hope this was where you where looking for.

Cor
..
\\\
Dim ds As New DataSet
Dim dt As New DataTable("Ruca")
For i As Integer = 0 To 7
Dim dc As New DataColumn(Chr(i + 65))
dt.Columns.Add(dc)
Next
'And 12 rows every column filled with the same letter
For i As Integer = 0 To 11
dt.Rows.Add(dt.NewRow)
For y As Integer = 0 To dt.Columns.Count - 1
dt.Rows(i)(y) = Chr(i + 48)
Next
Next
ds.Tables.Add(dt)
datagrid1.datasource=dt.tables("Ruca")
datagrid1.databind
////

I hopethis helps?
Cor
 
Hi Ruca,

You can put it in a session just by
session("ds") = ds

Updating a field can be something as (typed here watch typos)
\\\\
dim ds as new dataset = session("ds")
ds.tables(0).rows(index).item(fieldindex) = myvalue
///
or if you do a lot you can make it yourself more simple by
\\\
dim dr as datarow = ds.tables(0).rows(inded)
dr(fieldindex) = myvalue
///
I assume than you know the rest yourself?

If not feel free to ask again.

Cor
 
I have this:

Dim dt As DataTable
dt = Session.Item("myDt")

strCd = CStr(CType(E.Item.FindControl("edCod"), TextBox).Text)
dt.Rows(E.Item.ItemIndex).Item("Cod") = strCd

dgTeste.DataSource = dt
dgTeste.DataBind()

Session.Item("myDt") = dt

but it gives me an error saying: "Column 'Cod' is read only."
 
Hi Ruca,

I did not try it this way, however did you try it with a
Dim dt As New DataTable
dt = Session.Item("myDt")

Now you have set a reference to your Session item in my idea.

When it does not work give a message, than I check it deeper.

Cor
 
It gives me the same error.


Cor Ligthert said:
Hi Ruca,

I did not try it this way, however did you try it with a

Now you have set a reference to your Session item in my idea.

When it does not work give a message, than I check it deeper.

Cor
 
Hi Ruca,

I was not able to do it completly dynamicly, however here a sample with what
I have strugled this morning. (And it works)

I hope it works for you as well?

Cor


\\\\In the HTML page in the datagrid
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn DataField="A"></asp:BoundColumn>
<asp:BoundColumn DataField="B"></asp:BoundColumn>
<asp:BoundColumn DataField="C"></asp:BoundColumn>
</Columns>
///
\\\
Dim dt As New DataTable("Ruca")
Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
For i As Integer = 0 To 2
Dim dc As New DataColumn(Chr(i + 65))
dt.Columns.Add(dc)
Next
For i As Integer = 0 To 9
dt.Rows.Add(dt.NewRow)
For y As Integer = 0 To dt.Columns.Count - 1
dt.Rows(i)(y) = Chr(i + 48)
Next
Next
DataGrid1.DataSource = dt
DataGrid1.DataKeyField = dt.Columns(0).ColumnName
DirectCast(DataGrid1.Columns(1), BoundColumn).ReadOnly = True
DataGrid1.DataBind()
Session.Item("dt") = dt
Else
dt = DirectCast(Session.Item("dt"), DataTable)
DataGrid1.DataSource = dt
DirectCast(DataGrid1.Columns(1), BoundColumn).ReadOnly = True
DataGrid1.DataKeyField = dt.Columns(0).ColumnName
End If
End Sub
Private Sub DataGrid1_CancelCommand1(ByVal source _
As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
Private Sub DataGrid1_EditCommand1(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.UpdateCommand
Dim dv As New DataView(dt)
dv.RowFilter = dt.Columns(0).ColumnName & "=" _
& DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
dv(0)(1) = DirectCast(e.Item.Cells(2).Controls(0), TextBox).Text()
dv(0)(2) = DirectCast(e.Item.Cells(3).Controls(0), TextBox).Text
Session.Item("dt") = dt
End Sub
End Class
///
 
Back
Top