DATAGRIDVIEW AND GRIDVIEW OF DEVEXPRESS - devexpress

i was using datagridview in my erp but one of my new customer requested me to use devexpress
now in datagridview i was using a code as follow
Dim items As Boolean = False
For Each row In DataGridView1.Rows
If TextBox1.Text = row.Cells("Barcode").Value Then
items = True
Exit For
End If
Next
in line " For Each row In DataGridView1.Rows"
i am not able to write this code in devexpress gridcontrol.
how i can do it i mean how i can write the code somethhing like "gridview.rows"
my complete code is that i want to change for DEVEXPRESS --- GRIDVIEW
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
Dim items As Boolean = False
' For Each row In DATAGRIDVIEW.ROW
If DebitaccountTextEdit.Text = row.Cells("Barcode").Value Then
items = True
Exit For
End If
Next
If items = False Then
DataGridView1.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text)
MessageBox.Show(Me, "Item Added to List", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
Else
MessageBox.Show(Me, "Item Already Added", "System Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If

The DevExpress GridView does not have a Rows collection. You should instead loop through the rows with a for loop. See the Tutorial: Identifying Rows for more information about this. For example:
for (int i = 0; i < gridView1.RowCount; i++)
{
object barCodeValue = gridView1.GetRowCellValue(i, "Barcode");
}
Use the GridView's GetRowCellValue method to retrieve the value of a cell based on the field/column name and the row index.
Use the GridView's SetRowCellValue to set the value of a cell based on the field/column name and row index.
See also: Cell Values, Editors, and Validation

Related

How to avoid exception There is no row at position in VB.NET

I have this condition:
If IsNothing(ds.Tables(0).Rows(rowData).Item("DATE")) Then
Else
txtDATE.Text = ds.Tables(0).Rows(rowData).Item("DATE")
End If
What ever I do it is the same error.
How can I resolve this?
I have a loop that is going until 10 but my data set can sometimes has
less than 10 rows. I want to create TextBox control dinamically and to
set it text from data set but if for 10 row data set doesn't have data
just to leave text of the TextBox empty
Then use an If to check that condition:
For rowData As Int32 = 0 To 10
' .....
If ds.Tables(0).Rows.Count > rowData Then
Dim dt As Date? = ds.Tables(0).Rows(rowData).Field(Of Date?)("DATE")
txtDATE.Text = If(dt.HasValue, dt.Value.ToString(), "")
Else
txtDATE.Text = ""
End If
Next
I think the column is a nullable date-column(because you're using IsNothing). Then i'd prefer using a Date?, therefore you can use DataRowExtensions.Field which supports nullable types.
Instead of 10 textboxes you can use only one DataGridView control.
Add one column which represent your Date value (this can be done in designer too)
Dim column As New DataGridViewTextBoxColumn With
{
Header = "Date"
DataPropertyName = "DATE"
}
yourDataGridView.Columns.Add(column)
yourDataGridView.AutoGenerateColumns = False
Then use your DataTable as DataSource
yourDataGridView.DataSource = ds.Tables(0)
DataGridView will generate rows only for existed data in DataTable.
You can customize "outfit" of DataGridView your needs.

How to compare row values of two gridviews with dropdownlist column in asp.net?

I have two gridviews. The first one has a dropdownlist. When a user clicks a button named 'Show' data will be displayed on both gridviews where data comes from database. The row data on column with dropdownlist must be compared to the rows on the first column of the second gridview. If they are equal a messagebox will prompt saying that there's no changes and data will not be saved, else if they are not equal modal pop-up will be displayed asking if data are correct.
Below is my code for comparing but it only reads the value of the 1st row in gridview1.
For i = 0 To GridView1.Rows.Count - 1
Dim ddl As DropDownList = DirectCast(GridView1.Rows(i).Cells(6).FindControl("dropdowncriteria"), DropDownList)
Dim txt As TextBox = DirectCast(GridView1.Rows(i).Cells(7).FindControl("txtreason"), TextBox)
If ddl.SelectedValue = GridView2.Rows(i).Cells(0).Text And txt.Text = GridView2.Rows(i).Cells(1).Text Then
MessageBox("No Changes Made! Nothing will be Saved.")
Return
Else
lblmsg.Text = "Are you sure that all the Data you've Selected/Entered are Correct?"
mdlpopupmsg.Show()
Return
End If
Next
What must be the problem on this?
Thanks in advance.
It only reads the first value (i=0) because the return statements cause the for loop to exit after the first comparison. If you want to compare all the rows you will need a variable to keep track of the result of the if test for each row. Something like this:
Dim hasChanges As Boolean = False
For i = 0 To GridView1.Rows.Count - 1
...
If ddl.SelectedValue = GridView2.Rows(i).Cells(0).Text And txt.Text = GridView2.Rows(i).Cells(1).Text Then
'do nothing
Else
hasChanges = True
End If
Next
If hasChanges Then
MessageBox("Has changes.")
Else
MessageBox("No changes.")
End If
Dim itemt As Double
If (DataGridCart.RowCount() > 0) Then
For i = 0 To DataGridCart.Rows.Count - 1
'if itemt as double
itemt = Val(Trim(txtItem.Text))
If ((DataGridCart.Rows(i).Cells("Item").Value).Equals(itemt)) Then
MsgBox("existing entry")
End If
Next
End If

How to select the Checked rows in GridviewRow and display it in next page in VB.net

I am trying to get the rows from the gridviewrow and display it in the detail view in next page. I am using Asp.net with VB
Simply iterate the collection and check the status. You can then put the selected rows into a session object or something else to pass on to the next page.
For Each Row As GridViewRow In MyGrid.Rows
Dim SelectCheck As CheckBox = DirectCast(Row.FindControl("chkSelectForDetail"), CheckBox)
If SelectCheck.Checked
' Add your logic here to save the data or passing on to the detail page.
End If
Next

Format BoundField Data Types Dynamically

I'm using a CheckBoxList to define which columns are displayed in a GridView. I populate the CheckBoxList using a query like
select column_name, data_type from information_schema.columns
where table_name = 'myTable'
After the user has chosen the columns (with various Data Types in them) they wish to display, they press a button and the following snippet of VB code generates the GridView.
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
'PRODUCES BUGGY RESULTS BECAUSE APPLIED TO ALL BoundFields
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
bf.DataField = item.Value
bf.HeaderText = item.Value
bf.SortExpression = item.Value
statusReportGrid.Columns.Add(bf)
End If
Next
What I want to do is to apply the DataFormatString ONLY to the columns with a 'date' Data Type in them. However, I have found no way of programmatically determining the Type of the data in the column being bound, no any way of passing this info to the Control. This information exists in the information_schema, as shown in my query, but I don't know how to extract that out and use it to dynamically setup my BoundFields. It is important to note that I'm using an SqlDataSource.
I've experimented with just about every possible solution I can think of and end up here.
Thanks in advance for your help, it is VERY appreciated :)
If you set your check box list like this:
<asp:checkboxlist id="list" runat="server"
DataTextField="column_name" DataValueField="data_type" DataSourceID="YourDSID" />
You should be able to iterate through the items and check the value of the current Item like so:
For Each item As ListItem In chooseColsList.Items
If item.Selected Then
Dim bf As New BoundField()
If StrComp(item.Value,"date") = 0 Then 'Or however datetime is returned
bf.DataFormatString = "{0:dd-MMM-yyyy}"
bf.ApplyFormatInEditMode = True
End If
bf.DataField = item.Text
bf.HeaderText = item.Text
bf.SortExpression = item.Text
statusReportGrid.Columns.Add(bf)
End If
Next

Error in DropDownList using ASP.NET

I have a DropDownList called (DDL) in ASP.net page, I want that DDL contains some records of a table in the data base.
So I did this :
DDL.DataSource = myDataReader
DDL.DataBind()
But it's giving me (5 records) "the number of records of the table" but like this :
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
System.Data.Common.DataRecordInternel
You should set DataTextField and DataValueField, otherwise data binding will perform .ToString() on every row and put it as item:
DDL.DataSource = myDataReader;
DDL.DataTextField = "[Text column name]";
DDL.DataValueField = "[Value column name]";
DDL.DataBind();
you have to set the text and the key fields of the ddl before you databind
DDL.DataTextField = "textColumn";
DDL.DataValueField = "textColumn":
The code : ddl.datasource=reader is just setting the content present in reader (array of columns of table) as the main source of data.Now as ddl show only a single column in it so u need to write a piece of code which tells ddl that which column it has to display.So you will write: ddl.textfield="column name which you want to show";
and ddl.valuefield="column name which you want as a reference to pass to database";

Resources