GridView控件,背景色的改变和输出单元位置(COL和行)到文本框的VB.net鼠标点击单细胞 [英] VB.net Mouse click single cell of gridview, change backcolor and output cell position (col and row) to textbox

查看:341
本文介绍了GridView控件,背景色的改变和输出单元位置(COL和行)到文本框的VB.net鼠标点击单细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView中的表单,我想知道我可以在任何地方在GridView中使用鼠标点击选择单个单元格。

选中单元格背景颜色,然后切换到一个特定的颜色和形态上的一个文本框显示的行数和列数,我将作为参数传递给存储过程。

当一个随后的细胞被选择的最后选择的细胞恢复到它的原始颜色和新的细胞的背景颜色被改变并且文本框更新到新的小区的行号和列号。

到目前为止,我得到的最接近是选择一整排,但即使这只会影响该行的第一个单元格背景明智的。下划线影响行中的所有单元格。

 受保护的覆盖分渲染(BYVAL作家作为System.Web.UI.HtmlTextWriter)
    对于每一行作为GridViewRow在gvProgressGrid.Rows
        如果row​​.RowType = DataControlRowType.DataRow然后
            row.Attributes(onclick事件)=this.style.cursor ='指针'; this.style.ine'; this.style.backgroundColor ='#EEE'
        万一
    下一个    MyBase.Render(作家)
结束小组


解决方案

基本上是:在code你后面设置一个onclick脚本每个单元,通过单元格的坐标和对结果的文本框

在的aspx的JS脚本中的文本写入该点击单元的坐标,迭代的所有细胞中的表设置的颜色为白色,最后设置backgrond颜色为红色只为点击细胞

ASPX code:

 <%@页面语言=VBAutoEventWireup =false的codeBehind =TestEvidenziazione.aspx.vb继承=Web_Test_2010.TestEvidenziazione%GT;!< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    <脚本类型=文/ JavaScript的>
        功能亮点(my_row,细胞,文本框){
            的document.getElementById(&下;%= txtCellSelected.ClientID%gt;中)。值= my_row +','+细胞;
            变种表=的document.getElementById(&下;%= GridView1.ClientID%gt;中);
            对于(VAR I = 0,排;排= table.rows [I];我++){
                //通过迭代行
                //行将使用指定的for循环的行的变量进行访问
                对于(VAR J = 0,列;西= row.cells [J]。J ++){
                    //通过迭代列
                    //列将使用在指定的for循环的山坳变量进行访问
                        col.style.backgroundColor ='#FFFFFF';
                    如果(我== my_row&放大器;&安培;Ĵ==细胞){
                        col.style.backgroundColor =#FF0000;
                    }
                }
            }
        }
    < / SCRIPT>
< /头>
<身体GT;
    <表ID =form1的=服务器>    < D​​IV>
        < ASP:文本框ID =txtCellSelected=服务器>< / ASP:文本框>
    < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =真ShowHeader =false的>    < / ASP:GridView的>
    < / DIV>
    < /表及GT;
< /身体GT;
< / HTML>

vb.net code:

 公共类TestEvidenziazione
    继承System.Web.UI.Page    保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
        LoadData()
    结束小组    私人小组LoadData()
        昏暗的列表作为新的ArrayList
        昏暗ROW1作为新myRowClass
        昏暗ROW2作为新myRowClass
        昏暗ROW3作为新myRowClass        row1.a =0,0
        row1.b =0,1
        row1.c =0,2        row2.a =1,0
        row2.b =1,1
        row2.c =1,2        row3.a =2,0
        row3.b =2,1
        row3.c =2,2
        list.Add(ROW1)
        list.Add(ROW2)
        list.Add(ROW3)
        GridView1.DataSource =名单
        GridView1.DataBind()    结束小组    私有类myRowClass
        公共财产的作为字符串
        公共属性b以字符串
        公共属性c以字符串
    末级    私人小组GridView1_ preRender(发送者为对象,E作为System.EventArgs)把手GridView1。preRender
        对于index_row = 0〜GridView1.Rows.Count - 1
            如果GridView1.Rows(index_row).RowType = DataControlRowType.DataRow然后
                对于index_cell = 0〜GridView1.Rows(index_row).Cells.Count - 1
                    GridView1.Rows(index_cell).Attributes(的onclick)=高光((index_row).Cells&放大器; index_row.ToString&放大器;,&放大器; index_cell.ToString&放大器;,&放大器; txtCellSelected.ClientID&放大器; );
                下一个
            万一
        下一个
    结束小组
末级

I have a gridview in a webform and I would like to know how I can select a single cell anywhere in that gridview using a mouse click.

The selected cell's background colour then changes to a specific colour and a textbox on the form displays the row number and column number which I will pass as parameters to a stored procedure.

When a subsequent cell is selected the last selected cell reverts back to it's original colour and the new cell's background colour is changed and the textbox updates to the new cell's row number and column number.

The closest I've got so far is selecting a whole row, but even this only affects the first cell of the row background-wise. The underline affects all cells in the row.

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    For Each row As GridViewRow In gvProgressGrid.Rows
        If row.RowType = DataControlRowType.DataRow Then
            row.Attributes("onclick") = "this.style.cursor='pointer';this.style.ine';this.style.backgroundColor ='#EEE'"
        End If
    Next

    MyBase.Render(writer)
End Sub

解决方案

Basically: in the code behind you set an onclick script for each cell, passing the coordinates of the cell and the textbox for the results.

In the aspx the js script writes the coordinates of the clicked cell in the textbox, iterates all the cells in the table setting the color to white and finally sets the backgrond color to red only for the clicked cell

aspx code:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestEvidenziazione.aspx.vb" Inherits="Web_Test_2010.TestEvidenziazione" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function highlight(my_row, cell, textbox) {
            document.getElementById("<%=txtCellSelected.ClientID%>").value = my_row + ',' + cell;
            var table = document.getElementById("<%=GridView1.ClientID%>");
            for (var i = 0, row; row = table.rows[i]; i++) {
                //iterate through rows
                //rows would be accessed using the "row" variable assigned in the for loop
                for (var j = 0, col; col = row.cells[j]; j++) {
                    //iterate through columns
                    //columns would be accessed using the "col" variable assigned in the for loop
                        col.style.backgroundColor = '#ffffff';
                    if (i == my_row && j == cell) {
                        col.style.backgroundColor = '#ff0000';
                    }
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">

    <div>
        <asp:TextBox ID="txtCellSelected" runat="server"></asp:TextBox>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ShowHeader="false">

    </asp:GridView>
    </div>
    </form>
</body>
</html>

vb.net code:

Public Class TestEvidenziazione
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadData()
    End Sub

    Private Sub LoadData()
        Dim list As New ArrayList
        Dim row1 As New myRowClass
        Dim row2 As New myRowClass
        Dim row3 As New myRowClass

        row1.a = "0,0"
        row1.b = "0,1"
        row1.c = "0,2"

        row2.a = "1,0"
        row2.b = "1,1"
        row2.c = "1,2"

        row3.a = "2,0"
        row3.b = "2,1"
        row3.c = "2,2"
        list.Add(row1)
        list.Add(row2)
        list.Add(row3)
        GridView1.DataSource = list
        GridView1.DataBind()

    End Sub

    Private Class myRowClass
        Public Property a As String
        Public Property b As String
        Public Property c As String
    End Class

    Private Sub GridView1_PreRender(sender As Object, e As System.EventArgs) Handles GridView1.PreRender
        For index_row = 0 To GridView1.Rows.Count - 1
            If GridView1.Rows(index_row).RowType = DataControlRowType.DataRow Then
                For index_cell = 0 To GridView1.Rows(index_row).Cells.Count - 1
                    GridView1.Rows(index_row).Cells(index_cell).Attributes("onclick") = "highlight(" & index_row.ToString & "," & index_cell.ToString & ", " & txtCellSelected.ClientID & "); "
                Next
            End If
        Next
    End Sub
End Class

这篇关于GridView控件,背景色的改变和输出单元位置(COL和行)到文本框的VB.net鼠标点击单细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆